Provider State Management
Provider: a simplified state management solution
Provider is a Flutter package that simplifies state management by offering a straightforward approach based on the InheritedWidget mechanism. It allows data to be passed down the widget tree efficiently and notifies dependent widgets when the data changes.
Key Concepts
ChangeNotifier: A class that extends
ChangeNotifier
and represents the state. It provides thenotifyListeners()
method to notify listeners about state changes.Provider: An InheritedWidget that provides the state to its descendants. It listens to
ChangeNotifier
updates and rebuilds widgets that depend on the state.
Implementing Provider in Flutter
Add the
provider
package to your Flutter project's dependencies.Create a class that extends
ChangeNotifier
to represent the state.Wrap the root widget of your application with the
MultiProvider
widget to establish the provider hierarchy.Within the widget tree, use
Provider.of<T>(context)
to access the state and rebuild widgets when the state changes.Use
Consumer<T>
to consume the state and rebuild specific widgets when the state changes.
Working with ChangeNotifier
ChangeNotifier classes are responsible for holding the application state and notifying listeners about state changes. To update the state, modify the properties of the ChangeNotifier subclass and call notifyListeners()
.
Consuming Provider using Provider.of and Consumer
Use
Provider.of<T>(context)
to obtain the current state within a widget. It rebuilds the widget whenever the state changes.Use
Consumer<T>
to listen to changes in the state and rebuild only the specific subtree wrapped by theConsumer
widget.
For detailed information on Provider, navigate through the below reference links:
Last updated