Clean Flutter Network Architecture Using Dio (2022) | Part 5 — Api Service

Abdur Rafay Saleem
Flutter App Development
2 min readJul 4, 2022

Missed out on previous part? Check out Part 4 — Api Service

Flutter Dio Networking Architecture

Api Service

Lastly, we will create a service class called api_service.dart. This internally uses dioService to make network calls, performs JSON parsing as well as error handling to CustomException type.

Class Members

It holds an instance of dio service:

/// An instance of [DioService] for network requests
late final DioService _dioService;
/// A public constructor that is used to initialize the API service
/// and setup the underlying [_dioService].
ApiService(DioService dioService) : _dioService = dioService;

Class Methods

The entire class with the network methods looks like this:

In this file you may encouter different parameters to methods and here’s what they mean generally:

  • endpoint : The path for the request.
  • data : The request body to send.
  • queryParams : The query parameters to append to url.
  • cancelToken : To cancel this specific request.
  • requiresAuthToken : To decide whether to append Authorization token in headers or not.
  • converter : This is a callback that is used to decide how our response will parsed.

The GET methods contain two additional parameters:

  • cachePolicy : To decide the caching strategy.
  • cacheAgeDays : To decide how many days the cache is valid if a cache strategy is set.

Example Usage In Repository

This is an example of how you can use this entire layer together. Take a look at this method that receives list of posts from /posts endpoints:

Future<List<PostModel>> fetchAllPosts({
JSON? queryParameters,
}) async {
return _apiService.getCollectionData<PostModel>( // 1
endpoint: ApiEndpoint.posts(PostEndpoint.BASE), // 2
queryParams: queryParameters, // 3
converter: PostModel.fromJson, // 4
);
}
  1. Pass the return type PostModel to apiService.
  2. Pass the endpoint using the ApiEndpoint helper class.
  3. Pass any queryParameters into the apiService.
  4. Pass the factory constructor PostModel.fromJson to converter for deserialization.

However, this is just how I use this layer and you can use the ApiService in some other way that suits you.

Summary

With this final part we have completed our extensive guide and ended up with a beautiful, generic and reusable piece of code that can be copied over to any project you want or even converted into a private sub package. As you can see from the final example things look really clean and easy to read.

I encourage you guys to share your views regarding this article and help me improve on any lacking areas. If you liked this article, then please clap on this and share it with your network.

Check out this layer in action in the UniPal Github repository

--

--

Abdur Rafay Saleem
Flutter App Development

Flutter enthusiast by day, flutter enthusiast by night. A passionate computer science student with a focus on learning new technologies.