Handling api requests with Flutter Provider

Maneesha Erandi
3 min readSep 8, 2020
Photo by Jess Bailey on Unsplash

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.

dependencies:   
provider: ^4.3.2+2

Install the package. Run the command to get the package.

flutter pub get

import the package in to your file.

import 'package:provider/provider.dart';

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.

--

--