Maya Mohite
Globant
Published in
3 min readJan 14, 2020

--

Retrofit implementation in Flutter

To call Rest API's by sending dynamic headers, parameters, print request and response in a more custom and secured way “Retrofit” is the best solution. To integrate retrofit in your project add below dependencies in pubspec.yaml file.

  1. Dependencies:

run command “flutter pub get” to download the dependencies.

2. Create an abstract class to add base URL, retrofit configuration. This is the class I have created in my application with base URL, connection timeout and response timeout.

Retrofit generates the part file in the same folder as ApiClient. This file has logic to handle API calls.

Part file allows you to split a file into multiple dart files.

initially, the part file is not generated so it will give an error at _ApiClient. To create part file run command flutter pub run build_runner build it will show you warning “Missing “part ‘api_client.g.dart’;” Now add a part file in ApiClient i.e. part ‘package:api_client.g.dart’; Again run the same command and part file will be generated.

If you made any changes in ApiClient then also run the same command to update part file or run command flutter pub run build_runner watch it watches changes in your project files and automatically builds the files whenever needed. It is safe to start watcher once and leave running in the background

3. API declaration

Annotations on method and URL parameters decide how the request will be handled. Every method must have HTTP annotation and relative parameters. There are built-in annotations like GET, PUT, POST, PATCH, DELETE & HEADER. Add methods with annotations in ApiClient like below.

Method parameters:

@Path- To update the URL dynamically replacement block surrounded by { } must be annotated with @Path using the same string.
@Body- Sends dart object as the request body.
@Query- used to append the URL.
@Headers-to pass the headers dynamically.

4.Converter: We are getting a response from API in JSON format which we have to convert in dart class. There are two ways to parse JSON -

4.1 Manual Serialization: Retrofit automatically converts the “JSON” response to “Post” using Post.fromJson method.

post.dart

4.2 Automatic Serialization:

post.dart

Dependencies:

get dependencies and run flutter pub run build_runner build to create the part file and import it in the class. Generated part file has the same name as dart file ie “post.g.dart”

This serialization approach is better for medium to large projects as we don’t need handwritten boilerplate code and typos. The downside of this approach is we need initial setup and also for each dart class part file is created which might produce visual clutter in the project.

5. Error Handling :

Most of the time while performing network operations we get exceptions like SocketConnectionException, ConnectionTimeout and Invalid status code. Retrofit throws DioError which we have to handle to avoid application to be killed. I have written a ServerError class to handle exceptions.

6. Response:

API response should be generic so we can get error and success response by using a single class. This is the base model to get generic responses.

Everything is done now let’s perform the network operation:

This is all about Rest API implementation in the flutter. In the next blog, we will see how to modify request & response logs.

Thank’s for reading!

References :

--

--