Clean Network Layer in Flutter [Dio + Freezed + Json_Annotation]
Dear Friends, In this article, I will show the network usage that I use in my own projects. For the sake of simplicity, I will not use any package for state management.
Packages used
Our Network Layers
As a working principle, the Executer layer is run, then a request is made for the client in the Network Creator layer. If successful, the Decoding layer is passed. The data obtained in the decoding layer is converted to the Response model. Finally, a return value of type Result is provided.
All layers have their own error types.
- Request (Thrown if the request is incorrect)
- Type (Thrown in case of error while converting to response model in Decoding layer)
- Connectivity (In the Network Executer layer, controls are provided about whether there is an internet connection. If there is no internet connection, it throws an error back)
Contracts
We need to write contracts for our Client and Response models. In this way, we will be able to work under the same roof.
- BaseNetworkModel (Response models must derive from this class)
- BaseClientGenerator (Clients must derive from this class)
Network Options
The class we create to be able to use different properties in requests. You can add the options you want according to your needs.
Layers
- Network Creator
A request instance is created with the given route values.
- Network Decoder
The data value is checked whether it is a list or an object and it is converted into a response model.
- Network Executer
This is the layer that runs all our processes. First of all, internet control is done, then a request instance is created and called. The data from the server is converted to the response model.
Freezeds
- Result (returns Success or Failure)
- NetworkError (returns request,type,connectivity)
Clients
Whichever API you use has more requests (GET or POST), you can set a default value with the orElse method. This way you don’t need to specify it for each route extra.
You can use the plugin I created for visual code to create an easier client.
Response Models
- PostModel
- UserModel
Service
Network executer parameters
execute<T extends BaseNetworkModel, K>(route:BaseClientGenerator)
- route : Route defined by client
- T : Response type.
- K : It specifies how the response type value should be. List or object.
In the service below, we specify the K type as a list because the data that is returned from the API is a list of objects
Use Of
In this example, no package is used for state management. The next post will be about clean architecture with MobX + Clean Network.
Display of Errors
You can more easily find which request and what type the error is.
The display of the error you will get if the type is given incorrectly in the response model class created as an example, and the console output.
int? In case of changing userId to bool
Debug Console
Application
Summary
With this usage, you can write your network layer once and use it in all your projects. All you have to do is to create a client suitable for your API. You can change and develop it according to your structure and needs.
Stay well until the next post 😊
Github