<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Flutter App Development - Medium]]></title>
        <description><![CDATA[This publication contains content aimed at providing useful knowledge to beginners to help them kick start their journey into Flutter Development. - Medium]]></description>
        <link>https://medium.com/flutter-app-development?source=rss----2f30bd027c24---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Flutter App Development - Medium</title>
            <link>https://medium.com/flutter-app-development?source=rss----2f30bd027c24---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 15:17:46 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/flutter-app-development" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Clean Flutter Network Architecture Using Dio (2022) | Part 5 — Api Service]]></title>
            <link>https://medium.com/flutter-app-development/advanced-generic-network-layer-in-flutter-using-dio-part-5-api-service-1c7ce22423a4?source=rss----2f30bd027c24---4</link>
            <guid isPermaLink="false">https://medium.com/p/1c7ce22423a4</guid>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[flutter-networking]]></category>
            <category><![CDATA[network-architecture]]></category>
            <category><![CDATA[flutter-api]]></category>
            <category><![CDATA[dart]]></category>
            <dc:creator><![CDATA[Abdur Rafay Saleem]]></dc:creator>
            <pubDate>Mon, 04 Jul 2022 14:30:16 GMT</pubDate>
            <atom:updated>2022-07-14T18:04:41.513Z</atom:updated>
            <content:encoded><![CDATA[<h3>Clean Flutter Network Architecture Using Dio (2022) | Part 5 — Api Service</h3><blockquote>Missed out on previous part? Check out <a href="https://medium.com/@a.rafaysaleem/advanced-generic-network-layer-in-flutter-using-dio-part-4-api-endpoint-6522007f8b9b">Part 4 — Api Service</a></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VzF2BdExEHu-YUmnRpQOLg.png" /><figcaption>Flutter Dio Networking Architecture</figcaption></figure><h3>Api Service</h3><p>Lastly, we will create a service class called api_service.dart. This internally uses dioService to make <strong>network calls</strong>, performs <strong>JSON parsing</strong> as well as <strong>error handling</strong> to CustomException type.</p><h4>Class Members</h4><p>It holds an instance of dio service:</p><pre>/// An instance of [DioService] for network requests<br>late final DioService _dioService;</pre><pre>/// A public constructor that is used to initialize the API service<br>/// and setup the underlying [_dioService].<br>ApiService(DioService dioService) : _dioService = dioService;</pre><h4>Class Methods</h4><p>The entire class with the network methods looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cd03f1fd9839733c216a25082f7e9994/href">https://medium.com/media/cd03f1fd9839733c216a25082f7e9994/href</a></iframe><p><strong><em>In this file you may encouter different parameters to methods and here’s what they mean generally:</em></strong></p><ul><li>endpoint : The path for the request.</li><li>data : The request body to send.</li><li>queryParams : The query parameters to append to url.</li><li>cancelToken : To cancel this specific request.</li><li>requiresAuthToken : To decide whether to append <strong>Authorization</strong> token in headers or not.</li><li>converter : This is a callback that is used to decide how our response will parsed.</li></ul><p><strong><em>The GET methods contain two additional parameters:</em></strong></p><ul><li>cachePolicy : To decide the caching strategy.</li><li>cacheAgeDays : To decide how many days the cache is valid if a cache strategy is set.</li></ul><h4>Example Usage In Repository</h4><p>This is an example of how you can use this entire layer together. Take a look at this method that receives list of <strong>posts</strong> from /posts endpoints:</p><pre>Future&lt;List&lt;PostModel&gt;&gt; fetchAllPosts({<br>    JSON? queryParameters,<br>}) async {<br>  return _apiService.getCollectionData&lt;PostModel&gt;( // 1<br>   endpoint: ApiEndpoint.posts(PostEndpoint.BASE), // 2<br>   queryParams: queryParameters, // 3<br>   converter: PostModel.fromJson, // 4<br>  );<br>}</pre><ol><li>Pass the return type PostModel to apiService.</li><li>Pass the endpoint using the ApiEndpoint helper class.</li><li>Pass any queryParameters into the apiService.</li><li>Pass the factory constructor PostModel.fromJson to converter for deserialization.</li></ol><p>However, this is just how I use this layer and you can use the ApiService in some other way that suits you.</p><h4>Summary</h4><p>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.</p><p>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 <strong>clap on this</strong> and share it with your network.</p><blockquote>Check out this layer in action in the <a href="https://github.com/cyntaria/UniPal-Frontend/tree/master/lib/src/core/networking">UniPal Github repository</a></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1c7ce22423a4" width="1" height="1" alt=""><hr><p><a href="https://medium.com/flutter-app-development/advanced-generic-network-layer-in-flutter-using-dio-part-5-api-service-1c7ce22423a4">Clean Flutter Network Architecture Using Dio (2022) | Part 5 — Api Service</a> was originally published in <a href="https://medium.com/flutter-app-development">Flutter App Development</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Clean Flutter Network Architecture Using Dio (2022) | Part 4 — Api Endpoint]]></title>
            <link>https://medium.com/flutter-app-development/advanced-generic-network-layer-in-flutter-using-dio-part-4-api-endpoint-6522007f8b9b?source=rss----2f30bd027c24---4</link>
            <guid isPermaLink="false">https://medium.com/p/6522007f8b9b</guid>
            <category><![CDATA[flutter-networking]]></category>
            <category><![CDATA[flutter-api]]></category>
            <category><![CDATA[network-architecture]]></category>
            <category><![CDATA[flutter-endpoint]]></category>
            <category><![CDATA[flutter]]></category>
            <dc:creator><![CDATA[Abdur Rafay Saleem]]></dc:creator>
            <pubDate>Mon, 04 Jul 2022 14:30:07 GMT</pubDate>
            <atom:updated>2022-07-10T19:55:33.864Z</atom:updated>
            <content:encoded><![CDATA[<h3>Clean Flutter Network Architecture Using Dio (2022) | Part 4 — Api Endpoint</h3><blockquote>Missed out on previous part? Check out <a href="https://medium.com/@a.rafaysaleem/advanced-generic-network-layer-in-flutter-using-dio-part-3-interceptors-71b4607e1246">Part 3 — Interceptors</a></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Selfiv11vnD1lyKEj4qOOA.png" /><figcaption>Flutter Dio Networking Architecture</figcaption></figure><h3>Api Endpoint</h3><p>Create a file called api_endpoint.dart</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2aaeb6d7e55ebb77c34c472cc7fca877/href">https://medium.com/media/2aaeb6d7e55ebb77c34c472cc7fca877/href</a></iframe><p><strong><em>This file contains:</em></strong></p><ol><li>Enums for different endpoint collections. For e.g. for StudentEndpoint</li></ol><ul><li>If we want to make a request to /students we will use BASE</li><li>If we want to query specific resource like /students/id we will use BY_ID</li><li>If we want a sub collection like /students/requests we will use REQUESTS</li></ul><p>2. ApiEndpoint class which contains:</p><ul><li>The baseUrl field, which should be passed the value of the base url of API e.g. <a href="http://www.some-api.com/v1"><strong>www.some-api.com/v1</strong></a></li><li>Static methods for each REST API collection.</li></ul><p><strong><em>Static methods return the path based on the enum and parameters passed. For example:</em></strong></p><ul><li>For getting a specific student i.e. /students/17855 we call</li></ul><pre>ApiEndpoint.students(StudentEndpoint.BY_ERP, erp: 17855)</pre><ul><li>For getting a student’s activities i.e. /students/17855/saved-activities<br>we call</li></ul><pre>ApiEndpoint.students(<br>  StudentEndpoint.SAVED_ACTIVITIES_BASE,<br>  erp: 17855,<br>)</pre><ul><li>For students/17855/saved-activities/3 we call</li></ul><pre>ApiEndpoint.students(<br>  StudentEndpoint.SAVED_ACTIVITIES_BY_ID,<br>  erp: 17855,<br>  extendedResourceId: 3,<br>)</pre><h4>Summary</h4><p>We have setup a flexible and scalable solution for managing our endpoints without involving <strong>Strings</strong>. We will now move on to the final part of this guide that glues together all of this layer using an Api Service.</p><p>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.</p><blockquote>Read on <a href="https://medium.com/@a.rafaysaleem/advanced-generic-network-layer-in-flutter-using-dio-part-5-api-service-1c7ce22423a4">Part 5: Api Service</a></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6522007f8b9b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/flutter-app-development/advanced-generic-network-layer-in-flutter-using-dio-part-4-api-endpoint-6522007f8b9b">Clean Flutter Network Architecture Using Dio (2022) | Part 4 — Api Endpoint</a> was originally published in <a href="https://medium.com/flutter-app-development">Flutter App Development</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Clean Flutter Network Architecture Using Dio (2022) | Part 2— Custom Exception and Response Model]]></title>
            <link>https://medium.com/flutter-app-development/advanced-generic-network-layer-in-flutter-using-dio-part-2-custom-exception-and-response-model-dbc7fa8ba012?source=rss----2f30bd027c24---4</link>
            <guid isPermaLink="false">https://medium.com/p/dbc7fa8ba012</guid>
            <category><![CDATA[flutter-networking-layer]]></category>
            <category><![CDATA[flutter-generic-response]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[dios]]></category>
            <category><![CDATA[flutter-generic-exception]]></category>
            <dc:creator><![CDATA[Abdur Rafay Saleem]]></dc:creator>
            <pubDate>Mon, 04 Jul 2022 14:29:55 GMT</pubDate>
            <atom:updated>2022-07-10T19:53:54.044Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>Missed out on previous part? Check out <a href="https://medium.com/@a.rafaysaleem/advanced-generic-network-layer-in-flutter-using-dio-part-1-dio-service-3bde755e70e">Part 1 — Dio Service</a></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QoAa_smiX-L1QYWa7vUucQ.png" /><figcaption>Flutter Dio Networking Architecture</figcaption></figure><h3>Custom Exception Model</h3><p>Create a file called custom_exception.dart</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/75060760c34f08c782b76b6ad304a8d4/href">https://medium.com/media/75060760c34f08c782b76b6ad304a8d4/href</a></iframe><p><strong><em>It contains the following parts:</em></strong></p><ul><li>_ExceptionType: An enum that contains different types of exceptions.</li><li>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.</li><li>fromParsingException : A factory to convert serialization errors.</li></ul><h3>Response Model</h3><p>Create a file called response_model.dart</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/678058f18f2c32775f89a596bc7ca59e/href">https://medium.com/media/678058f18f2c32775f89a596bc7ca59e/href</a></iframe><p><strong><em>It contains the following parts:</em></strong></p><ul><li>_ResponseHeadersModel : This class is used to represent response headers.</li><li>ResponseModel : This class represent the response and contains a generic data type that we pass at each request level.</li></ul><p>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.</p><h3>Summary</h3><p>We have built some <strong>generic error handling</strong> and <strong>response models</strong> to clean up our networking codebase and make IDE experience better. In the next part, we will utilize these models in the <strong>interceptors</strong>.</p><p>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.</p><blockquote>Read on <a href="https://medium.com/@a.rafaysaleem/advanced-generic-network-layer-in-flutter-using-dio-part-3-interceptors-71b4607e1246">Part 3: Interceptors</a></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dbc7fa8ba012" width="1" height="1" alt=""><hr><p><a href="https://medium.com/flutter-app-development/advanced-generic-network-layer-in-flutter-using-dio-part-2-custom-exception-and-response-model-dbc7fa8ba012">Clean Flutter Network Architecture Using Dio (2022) | Part 2— Custom Exception and Response Model</a> was originally published in <a href="https://medium.com/flutter-app-development">Flutter App Development</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Clean Flutter Network Architecture Using Dio (2022) | Part 3 — Interceptors]]></title>
            <link>https://medium.com/flutter-app-development/advanced-generic-network-layer-in-flutter-using-dio-part-3-interceptors-71b4607e1246?source=rss----2f30bd027c24---4</link>
            <guid isPermaLink="false">https://medium.com/p/71b4607e1246</guid>
            <category><![CDATA[dios]]></category>
            <category><![CDATA[refresh-token]]></category>
            <category><![CDATA[interceptors]]></category>
            <category><![CDATA[error-handling]]></category>
            <category><![CDATA[flutter]]></category>
            <dc:creator><![CDATA[Abdur Rafay Saleem]]></dc:creator>
            <pubDate>Mon, 04 Jul 2022 14:29:49 GMT</pubDate>
            <atom:updated>2022-07-10T19:54:40.328Z</atom:updated>
            <content:encoded><![CDATA[<h3>Clean Flutter Network Architecture Using Dio (2022) | Part 3 — Interceptors</h3><blockquote>Missed out on previous part? Check out <a href="https://medium.com/@a.rafaysaleem/advanced-generic-network-layer-in-flutter-using-dio-part-2-custom-exception-and-response-model-dbc7fa8ba012">Part 2 — Custom Exception and Response Model</a></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5L7AIfdomtcvVIASpKVmjg.png" /><figcaption>Flutter Dio Networking Architecture</figcaption></figure><h3>Interceptors</h3><p>Let’s add a few useful interceptors to handle some important network tasks for us. An <strong>interceptor</strong> fires automatically for each <strong>request</strong>, <strong>response</strong> or <strong>error</strong> depending on which method it overrides. We will create <strong>three</strong> interceptors.</p><h4><strong>Api Interceptor</strong></h4><p>Create a file called api_interceptor.dart . This is how it should look, the docs in the file are self explanatory:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7d0dff9d9cb9f98e53d4dd906ac31300/href">https://medium.com/media/7d0dff9d9cb9f98e53d4dd906ac31300/href</a></iframe><h4><strong>Logging Interceptor</strong></h4><p>Create a file called logging_interceptor.dart like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0a145e0b9619adb592c4f7bcc620e005/href">https://medium.com/media/0a145e0b9619adb592c4f7bcc620e005/href</a></iframe><h4><strong>Refresh Token Interceptor</strong></h4><p>Create a file refresh_token_interceptor.dart like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7f63447b7c89331e0ddf92dfdf7145c2/href">https://medium.com/media/7f63447b7c89331e0ddf92dfdf7145c2/href</a></iframe><p>You can add these interceptors while initializing DioService.</p><h4>Summary</h4><p>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.</p><p>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.</p><blockquote>Read on <a href="https://medium.com/@a.rafaysaleem/advanced-generic-network-layer-in-flutter-using-dio-part-4-api-endpoint-6522007f8b9b">Part 4: Api Endpoint</a></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=71b4607e1246" width="1" height="1" alt=""><hr><p><a href="https://medium.com/flutter-app-development/advanced-generic-network-layer-in-flutter-using-dio-part-3-interceptors-71b4607e1246">Clean Flutter Network Architecture Using Dio (2022) | Part 3 — Interceptors</a> was originally published in <a href="https://medium.com/flutter-app-development">Flutter App Development</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Clean Flutter Network Architecture Using Dio (2022) | Part 1— Dio Service]]></title>
            <link>https://medium.com/flutter-app-development/advanced-generic-network-layer-in-flutter-using-dio-part-1-dio-service-3bde755e70e?source=rss----2f30bd027c24---4</link>
            <guid isPermaLink="false">https://medium.com/p/3bde755e70e</guid>
            <category><![CDATA[dio-architecture]]></category>
            <category><![CDATA[flutter-dio-caching]]></category>
            <category><![CDATA[flutter-networking]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[dios]]></category>
            <dc:creator><![CDATA[Abdur Rafay Saleem]]></dc:creator>
            <pubDate>Mon, 04 Jul 2022 14:29:43 GMT</pubDate>
            <atom:updated>2022-07-10T19:57:10.789Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>Missed out on previous part? Check out <a href="https://medium.com/flutter-app-development/advanced-generic-network-layer-in-flutter-using-dio-2022-2f362e22e6c9">Part 0 — Introduction</a></blockquote><figure><img alt="Flutter Dio Networking Architecture" src="https://cdn-images-1.medium.com/max/1024/1*Nj58nktAqHZawOWvcGZOGA.png" /><figcaption>Flutter Dio Networking Architecture</figcaption></figure><h3>Dio Service</h3><p>First, we will create a wrapper class around the Dio package called dio_service.dart</p><p><strong><em>It will contain 3 variables:</em></strong></p><pre>class DioService {</pre><pre>/// An instance of [Dio] for executing network requests.<br>final Dio _dio;</pre><pre>/// A set of cache options to be used for each request.<br>final CacheOptions? globalCacheOptions;</pre><pre>/// An instance of [CancelToken] used to pre-maturely cancel<br>/// network requests.<br>final CancelToken _cancelToken;</pre><p><strong><em>Next, we will create a constructor to initialize these variables:</em></strong></p><pre>/// A public constructor that is used to create a Dio service and initialize the underlying [Dio] client.<br>///<br>/// * [interceptors]: An [Iterable] for attaching custom<br>/// [Interceptor]s to the underlying [_dio] client.<br>/// * [httpClientAdapter]: Replaces the underlying<br>/// [HttpClientAdapter] with this custom one.</pre><pre>DioService({<br>  required Dio dioClient,<br>  this.globalCacheOptions,<br>  Iterable&lt;Interceptor&gt;? interceptors,<br>  HttpClientAdapter? httpClientAdapter,<br>})  : _dio = dioClient,<br>      _cancelToken = CancelToken() {<br>  if (interceptors != null) _dio.interceptors.addAll(interceptors);<br>  if (httpClientAdapter != null) <br>    _dio.httpClientAdapter =  httpClientAdapter;<br>}</pre><p><strong><em>This constructor takes in:</em></strong></p><ul><li>Dio Object: Used to combine network functionality and make requests.</li><li>CacheOptions(Optional): Used to set decide a caching strategy.</li><li><strong>List of </strong>Interceptors(Optional): These are added to the Dio object and are used for purposes like logging, refreshing token and error handling.</li><li>HttpClientAdapter (Optional): This replaces the underlying adapter for Dio. It is used to force usage of HTTP 2.0 protocol.</li></ul><h4>The HTTP methods</h4><p>Now let’s move on to implementing the methods <strong>GET, POST, PATCH, DELETE</strong>. Take a look at this file:</p><pre>Future&lt;ResponseModel&lt;R&gt;&gt; get&lt;R&gt;({<br>  required String endpoint,<br>  JSON? queryParams,<br>  Options? options,<br>  CacheOptions? cacheOptions,<br>  CancelToken? cancelToken,<br>}) async {<br>  final response = await _dio.get&lt;JSON&gt;(<br>    endpoint,<br>    queryParameters: queryParams,<br>    options: _mergeDioAndCacheOptions(<br>      dioOptions: options,<br>      cacheOptions: cacheOptions,<br>    ),<br>    cancelToken: cancelToken ?? _cancelToken,<br>  );<br>  return ResponseModel&lt;R&gt;.fromJson(response.data!);<br>}</pre><pre>Future&lt;ResponseModel&lt;R&gt;&gt; post&lt;R&gt;({<br>  required String endpoint,<br>  JSON? data,<br>  Options? options,<br>  CancelToken? cancelToken,<br>}) async {<br>  final response = await _dio.post&lt;JSON&gt;(<br>    endpoint,<br>    data: data,<br>    options: options,<br>    cancelToken: cancelToken ?? _cancelToken,<br>  );<br>  return ResponseModel&lt;R&gt;.fromJson(response.data!);<br>}</pre><pre>Future&lt;ResponseModel&lt;R&gt;&gt; patch&lt;R&gt;({<br>  required String endpoint,<br>  JSON? data,<br>  Options? options,<br>  CancelToken? cancelToken,<br>}) async {<br>  final response = await _dio.patch&lt;JSON&gt;(<br>    endpoint,<br>    data: data,<br>    options: options,<br>    cancelToken: cancelToken ?? _cancelToken,<br>  );<br>  return ResponseModel&lt;R&gt;.fromJson(response.data!);<br>}</pre><pre>Future&lt;ResponseModel&lt;R&gt;&gt; delete&lt;R&gt;({<br>  required String endpoint,<br>  JSON? data,<br>  Options? options,<br>  CancelToken? cancelToken,<br>}) async {<br>  final response = await _dio.delete&lt;JSON&gt;(<br>    endpoint,<br>    data: data,<br>    options: options,<br>    cancelToken: cancelToken ?? _cancelToken,<br>  );<br>  return ResponseModel&lt;R&gt;.fromJson(response.data!);<br>}</pre><p><strong><em>Each of these methods except GET contains the following common parameters:</em></strong></p><ul><li>String endpoint: The path after the <strong>baseUrl</strong>.</li><li>JSON data: The request body that needs to be sent.</li><li>Options options: Custom request options that are used to add headers, set custom timeouts, etc. for this specific request.</li><li>CancelToken? cancelToken: Used to cancel this specific request only, if not set, then the global <strong>cancelToken</strong> will be passed. The global one cancels all requests.</li></ul><p><strong><em>The GET method contains the following different parameters to customize querying and caching:</em></strong></p><ul><li>JSON queryParams: Instead of <strong>data</strong> it uses a set of query params that are added to the url internally.</li><li>CacheOptions? cacheOptions: This decides the caching strategy for this specific request and merges it with the global one and overrides the common settings.</li><li>Since dio required an <strong>Options</strong> object only, the cache options are merged with the passed Options using the method _mergeDioAndCacheOptions:</li></ul><pre>/// A utility method used to merge together [Options]<br>/// and [CacheOptions].<br>///<br>/// Returns an [Options] object with [CacheOptions] stored<br>/// in the [options.extra] key.<br>Options? _mergeDioAndCacheOptions({<br>  Options? dioOptions,<br>  CacheOptions? cacheOptions,<br>}) {<br>  if (dioOptions == null &amp;&amp; cacheOptions == null) {<br>    return null;<br>  } else if (dioOptions == null &amp;&amp; cacheOptions != null) {<br>    return cacheOptions.toOptions();<br>  } else if (dioOptions != null &amp;&amp; cacheOptions == null) {<br>    return dioOptions;<br>  }</pre><pre>final _cacheOptionsMap = cacheOptions!.toExtra();<br>  final options = dioOptions!.copyWith(<br>   extra: &lt;String, dynamic&gt;{...dioOptions.extra!, ..._cacheOptionsMap},<br>  );<br>  return options;<br>}</pre><p><strong><em>This is how the entire file looks like:</em></strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fcbb4e9dc79426bc20dfccbc0bec025d/href">https://medium.com/media/fcbb4e9dc79426bc20dfccbc0bec025d/href</a></iframe><h4>Creating an Instance</h4><p>Finally, this is how you create instance of this service:</p><pre>final baseOptions = BaseOptions(<br>  baseUrl: &quot;www.sample-url.com&quot;,<br>);</pre><pre>final _cacheOptions = CacheOptions(<br>  store: HiveCacheStore(pp.getApplicationDocumentsDirectory()),<br>  policy: CachePolicy.noCache, // Bcz we force cache on-demand in repositories<br>  maxStale: const Duration(days: 30), // No of days cache is valid<br>  keyBuilder: (options) =&gt; options.path,<br>);</pre><pre>DioService(<br>  dioClient: _dio,<br>  globalCacheOptions: _cacheOptions,<br>  interceptors: [<br>    // Order of interceptors very important<br>    ApiInterceptor(),<br>    DioCacheInterceptor(options: _cacheOptions),<br>    if (kDebugMode) LoggingInterceptor(),<br>    RefreshTokenInterceptor()<br>  ],<br>);</pre><p>The HiveCacheStore comes from the package <a href="https://pub.dev/packages/flutter_cache_manager_hive">flutter cache_manager_hive</a> and pp comes from <a href="https://pub.dev/packages/path_provider">path_provider</a>. You can use your choice of dependency injection service to provide this service to all layers.</p><h4>Summary</h4><p>We have built a highly proficient and generic dio service with internal support for caching and interceptors. However, this is just the first step in building our network layer and in the next part, we will see how to use this service effectively in combination with other parts.</p><p>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.</p><blockquote>Read on <a href="https://medium.com/@a.rafaysaleem/advanced-generic-network-layer-in-flutter-using-dio-part-2-custom-exception-and-response-model-dbc7fa8ba012">Part 2: Custom Exception and Response Model</a></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3bde755e70e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/flutter-app-development/advanced-generic-network-layer-in-flutter-using-dio-part-1-dio-service-3bde755e70e">Clean Flutter Network Architecture Using Dio (2022) | Part 1— Dio Service</a> was originally published in <a href="https://medium.com/flutter-app-development">Flutter App Development</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Clean Flutter Network Architecture Using Dio (2022) | Part 0 — Introduction]]></title>
            <link>https://medium.com/flutter-app-development/advanced-generic-network-layer-in-flutter-using-dio-2022-2f362e22e6c9?source=rss----2f30bd027c24---4</link>
            <guid isPermaLink="false">https://medium.com/p/2f362e22e6c9</guid>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[flutter-networking]]></category>
            <category><![CDATA[flutter-app-development]]></category>
            <category><![CDATA[dart]]></category>
            <category><![CDATA[flutter-dio]]></category>
            <dc:creator><![CDATA[Abdur Rafay Saleem]]></dc:creator>
            <pubDate>Mon, 04 Jul 2022 13:25:05 GMT</pubDate>
            <atom:updated>2022-07-10T19:56:52.248Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>Learn how to create the solid and flexible networking layer that can work with any REST API client and contains built in support for caching, serialization and interceptors.</blockquote><figure><img alt="Flutter Dio Networking Architecture" src="https://cdn-images-1.medium.com/max/1024/1*rkN65G4vz8Ndik5ArXzqUw.png" /><figcaption>Flutter Dio Networking Architecture</figcaption></figure><h3>Introduction</h3><p>Networking is a vital part of apps and requires an effortless and error free setup in Flutter. It is important to setup a generic and more importantly a reusable networking layer than can be used in any Flutter project without changing code.</p><p>If you are looking for such an implementation, look no further as after following this guide you will end up with a complete solid layer to work with any API.</p><p>This is a 5 part series broken down into the following sequence:</p><ul><li><a href="https://medium.com/@a.rafaysaleem/advanced-generic-network-layer-in-flutter-using-dio-part-1-dio-service-3bde755e70e"><strong>Part 1: Dio Service</strong></a></li><li><a href="https://medium.com/@a.rafaysaleem/advanced-generic-network-layer-in-flutter-using-dio-part-2-custom-exception-and-response-model-dbc7fa8ba012"><strong>Part 2: Custom Exception and Response Model</strong></a></li><li><a href="https://medium.com/@a.rafaysaleem/advanced-generic-network-layer-in-flutter-using-dio-part-2-custom-exception-and-response-model-dbc7fa8ba012"><strong>Part 3: Interceptors</strong></a></li><li><a href="https://medium.com/@a.rafaysaleem/advanced-generic-network-layer-in-flutter-using-dio-part-4-api-endpoint-6522007f8b9b"><strong>Part 4: Api Endpoint</strong></a></li><li><a href="https://medium.com/@a.rafaysaleem/advanced-generic-network-layer-in-flutter-using-dio-part-5-api-service-1c7ce22423a4"><strong>Part 5: Api Service</strong></a></li></ul><p>Each of these parts can be used independently, but together they complement each other and form a solid, fully functional networking layer.</p><h4>Prerequisites</h4><p>Before we get started, there are a few things you need to do to prepare for your code.</p><ol><li>Install the following dependencies:</li></ol><pre>dependencies:<br>  dio: ^4.0.6<br>  dio_cache_interceptor: ^3.2.7<br>  dio_cache_interceptor_hive_store: ^3.1.1<br>  freezed_annotation: ^2.0.3<br>  json_annotation: ^4.5.0<br>  path_provider: ^2.0.10</pre><p>2. Install the following dev_dependencies:</p><pre>dev_dependencies:<br>  build_runner: ^2.1.11<br>  freezed: ^2.0.3+1<br>  json_serializable: ^6.2.0</pre><p>3. Next, create a file called typedefs.dart and add this line:</p><pre>typedef JSON = Map&lt;String, dynamic&gt;;</pre><p>That is all for the introduction. If you want to skip the guide and move straight to code than check out the <strong>Networking</strong> folder in <a href="https://github.com/cyntaria/UniPal-Frontend/tree/master/lib/src/core/networking"><strong><em>UniPal</em></strong></a> GitHub repository for the full example.</p><blockquote>Read on <a href="https://medium.com/@a.rafaysaleem/advanced-generic-network-layer-in-flutter-using-dio-part-1-dio-service-3bde755e70e">Part 1: Dio Service</a></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2f362e22e6c9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/flutter-app-development/advanced-generic-network-layer-in-flutter-using-dio-2022-2f362e22e6c9">Clean Flutter Network Architecture Using Dio (2022) | Part 0 — Introduction</a> was originally published in <a href="https://medium.com/flutter-app-development">Flutter App Development</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>