0

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.

  1. It is safe to use StatelessWidget if we use a ChangeNotifierProvider inside of it?
  2. Is there any performance gain about using StatelessWidget vs StatefullWidget?
Daniel Gomez Rico
  • 15,026
  • 20
  • 92
  • 162

2 Answers2

2

"Safe" doesn't apply, since setState is a method on State class, and since StatelessWidget doesn't have one of those, this won't even come up. And there's no broad sweeping rules about performance. Try it, and see.

Randal Schwartz
  • 39,428
  • 4
  • 43
  • 70
  • Thanks for answering, I updated the question, you are totally right, my bad, I rushed into creating the question without thinking about my context, Im using a `Provider`, and it shows a widget that rebuild itself after some changes, it does not use `setState` at all – Daniel Gomez Rico Dec 16 '20 at 13:42
  • 1
    And now that you updated question 1, my answer is yes. There's still no state being held within the StatelessWidget, just a way to transform incoming data to a widget. – Randal Schwartz Dec 16 '20 at 16:42
0

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 be Statefull, that responsibility goes to the ChangeNotifierProvider widget.

Daniel Gomez Rico
  • 15,026
  • 20
  • 92
  • 162