Clean Flutter Network Architecture Using Dio (2022) | Part 2— Custom Exception and Response Model

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

Missed out on previous part? Check out Part 1 — Dio Service

Flutter Dio Networking Architecture

Custom Exception Model

Create a file called custom_exception.dart

flutter generic exception class

It contains the following parts:

  • _ExceptionType: An enum that contains different types of exceptions.
  • fromDioException : A factory to convert Dio and other errors to our custom error. This makes it easier to handle later when we only need to check one error type.
  • fromParsingException : A factory to convert serialization errors.

Response Model

Create a file called response_model.dart

flutter generic response model

It contains the following parts:

  • _ResponseHeadersModel : This class is used to represent response headers.
  • ResponseModel : This class represent the response and contains a generic data type that we pass at each request level.

This class prevents us from dealing with a Map object of response which can lead to errors in case a key is mistyped. It also allows debugger to infer type of our parsed JSON model and offer better code-completion in IDE.

Summary

We have built some generic error handling and response models to clean up our networking codebase and make IDE experience better. In the next part, we will utilize these models in the interceptors.

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 3: Interceptors

--

--

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.