I see that the flutter team advise this:
For example, in Android, this is similar to placing an ImageView with your logo. The logo is not going to change >during runtime, so use a StatelessWidget in Flutter. If you want to dynamically change the UI based on data received after making an HTTP call or user interaction then you have to work with StatefulWidget and tell the Flutter framework that the widget’s State has been updated so it can update that widget. https://flutter.dev/docs/get-started/flutter-for/android-devs#how-do-i-update-widgets
Im using provider, and it lets you use a ChangeNotifierProvider
to rebuild the widget tree.
If I have a StatelessWidget
and we use a ChangeNotifierProvider
, the build
function is still invoked multiple times when the ChangeNotifier
notify a change, and the widget rebuilds, which confuses me a lot about the difference between a StatelessWidget
and a StatefullWidget
.
- It is safe to use
StatelessWidget
if we use aChangeNotifierProvider
inside of it? - Is there any performance gain about using
StatelessWidget
vsStatefullWidget
?
It is a little bit confusing but this explanation may help:
Since the
ChangeNotifierProvider
will handle the state, then the parent (your widget) does not need to beStatefull
, that responsibility goes to theChangeNotifierProvider
widget.