Provider
Last updated
Last updated
With Providers and MultiProviders we can do easy StateManagement in Flutter.State Management in Flutter using Provider
State Management In Flutter made Easy
Go to your pubspec.yaml file add the below dependency,
I would recommend to add the latest dependency version, at this moment it is 4.0.4
The Providers will have
ChangeNotifiers
ChangeNotifierProviders
Consumers
The Basic flow to update a state in Provider is to call notifyListeners() on the Listener class which in turn calls the Change Notifier and then the corresponding model Consumer widget will be triggered with the Model Data.
So Don’t get confused, it’s really easy. Let’s see how that works.
So we have two screens here 1. Home Screen 2. Add Items Screen
We will be creating a Shopping List here and the user should be able to add data to the Shopping list from anywhere in the Flutter Application.
The Add Items Screen will have a Textfield and a button. When the user hits the button, he/she should be able to add data from the Add Item screen to the Home Screen and update the Shopping List in the Home Screen.
This can be achieved with the help of Providers.
First thing I need is a Notifier class for each of my DataModel.
Our model is called ‘Item’
We also need a Notifier class named “ItemAddNotifier” which will look like this
So this is an individual class and we have not linked it to anything yet.
In the above class, you can see that it has extended ‘ChangeNotifier’ which means whenever there is some trigger for this model from anywhere in the application for the model ‘ItemAddNotifier’, this class will be triggered.
Let’ check this in the AddItemScreen class below
Create a new File named AddItemScreen.dart
Here is the build method for this screen.
We are opening this screen from the Home Screen, and the Add Item button on this screen will close this screen and return to HomeScreen.
Here you can see we are calling
_itemNameController is the controller for the TextField.
This code will trigger the ‘ItemAddNotifier’ class and then you will have access to all the methods inside ‘ItemAddNotifier’ class. As you can see the above code the ‘addItem’ method is being called which calls the ‘addItem’ method in the ‘ItemAddNotifier’ class and add it to the data to the itemList in the class and then we are calling the important method ‘notifyListeners()’ which notifiers the ChangeNotifier that we have not written yet. Let’s write that.
So here we want to use the ItemAddNotifier across the application, not only for the HomeScreen.
For this reason, we need to add the ChangeNotifierProvider to the root of the Application.
Let’s go to the main.dart file which is the entry point of your Flutter Application.
So here the main.dart file will look like this.
In the above code, we are wrapping the whole Root Widget which is the MaterialApp widget inside the ‘ChangeNotifier’ widget. This makes sure that the whole application receives the updates whenever there is a change in the Shopping List.
Now it’s time to receive the Data anywhere in the application. In this example, we will consume it in the HomeScreen. The ‘Consumer’ widget with the correct ‘Notifier Class’ consumes the corresponding data. Here our Notifier is ‘ItemAddNotifier’
HomeScreen
In the above code you can see the ‘Consumer’ widget
Here the second parameter is our Notifier. From the parameter you can access all the members in the Notifier class.
And That’s it.
Advantages
Whenever there is change in data for this Model, only the Consumer widget corresponding to that Model will be rebuilt results in improved Performance.
You can have any number of Consumers and update anywhere from the application, and use less setState().
This is absolutely possible that you will have more than one data model and corresponding notifiers, in that case you can use ‘MultiProviders’.
Let’s say I have a ShopNameNotifier which looks something like this
So to add the Change Notifier, we will update the main.dart like this
Here you can see we added the ‘MultiProvider’ widget with two ‘ChangeNotifierProvider’ with two models.
To Update the ShopName, call like below from any class.
And to Consume the data is simple,
Thats it!!!
https://bitbucket.org/vipinvijayan1987/tutorialprojects/src/ProviderDemo/FlutterTutorialProjects/flutter_demos/Vipin Vijayan
Add Item Screen
Done, So you can see we are using setState() nowhere in the application.Shopping List Screen