Dio Interceptors in Flutter

Gonçalo Palma
Flutter Community
Published in
7 min readSep 8, 2019

--

Photo by Alina Grubnyak on Unsplash

Sometimes we don’t need complex apps, sometimes we just need an app that displays a list of items from one endpoint, and we can manage to do it with a simple method:

And we don’t have any errors, there’s no need to log our responses. What about cache? Nobody cares about it!

But truth be told, almost no apps are as simple as this. Some do require more in order for us to get the response from the server or debug our application, such as:

  • Sending dynamic headers to the server. For example, a stored key from shared preferences;
  • Checking each response header and saving its values locally;
  • Verifying the errors sent from the server and directly map them to Error classes that our app can handle;
  • Additionally, we might want to add a simplified cache to our app so that if the connection is timed out or if the user does not have internet access, we can display the previous response for that request;
  • And we can also add logging to all our responses and requests.

--

--

Gonçalo Palma
Flutter Community

Author, Flutter Lead Developer @Pvotal Tech. Organizer, Google Developer Expert for Flutter and Dart, Lead Flutter Developer. 🌍 https://gpalma.pt/