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.
ChangeNotifier: A class that extends ChangeNotifier
and represents the state. It provides the notifyListeners()
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.
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.
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()
.
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 the Consumer
widget.
For detailed information on Provider, navigate through the below reference links: