State management is very important when we work with flutter. Flutter provider is much better way to handle state in Flutter. The
provider package is easy to understand and it doesn’t use much code. It also uses concepts that are applicable in every other approach.
We don’t have to keep so many files to use provider and the implementation is really easy and simple.
Let’s get started. I will explain the api handling with a get request.
Let’s install the package. Always install the latest version of the package. Add the package in to your pubspec.yaml file.
Install the package. Run the command to get the package.
flutter pub get
import the package in to your file.
This is my data model to get the data from post api of jsonplaceholder. We can get the data with getSinglePostData api request. Since I wanted to give a toast when there is an error, I have passed context as a parameter to the request.
We can create our provider class with ChangeNotifier to notify all the listeners. You can create objects that you want to share with the other classes. Inside the function add notifyListeners() method to rebuild the widgets which listen to the object you want.
Add your main method like this. Wrap MyApp(the class you want) with MultiProvider from…