Home > other >  Are stateful widgets not supposed to be used when working with providers?
Are stateful widgets not supposed to be used when working with providers?

Time:11-12

This is more of a conceptual question. Going through the flutter documentation I "In Flutter, it makes sense to keep the state above the widgets that use it". Going through the rest of the doc and example code, it seems that the doc wants us to use inly providers as states and use stateless widgets that consume the providers.

Am I correct in understanding that statefull widgets are to be used against stateless providers or am I incorrect and we can use them both together to some other effect?

CodePudding user response:

Yes, you can use StatefulWidgets with Provider / any state management. Provider provides global variables that needs to be used multiple times on application, or that needs to be stored globally, so those Widgets do not need to be Stateful. But Widgets that their state doesn't affect other widgets or any other "stuff" on the application can manage their own state.

CodePudding user response:

Of course you can use them together. Sometimes you even have to, e.g. for storing a GlobalKey or TabController. Whether you put other state in there related to your business logic is completely up to you, but i don't see why you shouldn't if it makes sense for your particular use case.

In my own projects, i often mix state management approaches based on the complexity of a screen. If this is your first flutter project, i'd recommend starting out with stateful widgets and when you notice that it becomes too large or difficult to manage, you can always refactor it using Provider.

  • Related