Clean Flutter Network Architecture Using Dio (2022) | Part 3 — Interceptors

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

Missed out on previous part? Check out Part 2 — Custom Exception and Response Model

Flutter Dio Networking Architecture

Interceptors

Let’s add a few useful interceptors to handle some important network tasks for us. An interceptor fires automatically for each request, response or error depending on which method it overrides. We will create three interceptors.

Api Interceptor

Create a file called api_interceptor.dart . This is how it should look, the docs in the file are self explanatory:

Logging Interceptor

Create a file called logging_interceptor.dart like this:

Refresh Token Interceptor

Create a file refresh_token_interceptor.dart like this:

You can add these interceptors while initializing DioService.

Summary

We have leveraged the power of Dio Interceptors to automatically log, authorize and validate our network requests and responses. We can now finally move on the towards developing our API interaction by starting of with a helper class for endpoints.

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.

Read on Part 4: Api Endpoint

--

--

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.