Search code examples
flutterproviderstate-management

Why to use provider in state management while we can use Stateful builder widget?


So I was introduced to the idea of rebuilding a certain portion of a flutter application in order to display changes to the UI, and I understood that rebuilding the whole screen is expensive in terms of time, and heck, the app might rebuild the whole widget tree.

I've found that using a package called provider Provider package documentations

solves this problem easily, and I tried to bear with learning the new tech.

However, Flutter has announced a new widget called StatefulBuilder StatefulBuilder documentations, which does the same thing in theory, but I wonder if there is anything else to consider when choosing between the two?


Solution

  • StatefulBuilder is a good solution when it needs to rebuild a small portion of the screen. For example, in a rider app like Uber, the driver can change his availability status by a switch button. Here the state of the switch button is independent of the rest of the screen so to achieve this we can wrap our switch button inside a stateful button.

    But if we need to handle a complex state for a full screen or a list etc. then it is better to use a stateful widget or Provider for state management. The advantage of using a provider is that it allows us to separate the business logic into a separate class (acting like a view model).