<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: dklimkin</title>
    <description>The latest articles on DEV Community by dklimkin (@dklimkin).</description>
    <link>https://dev.to/dklimkin</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F835782%2Ff2071bce-2ab2-40cd-948b-1b797e0cc270.jpg</url>
      <title>DEV Community: dklimkin</title>
      <link>https://dev.to/dklimkin</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dklimkin"/>
    <language>en</language>
    <item>
      <title>Building a Generic and Performant Networking Layer in Flutter</title>
      <dc:creator>dklimkin</dc:creator>
      <pubDate>Thu, 24 Mar 2022 02:52:07 +0000</pubDate>
      <link>https://dev.to/dklimkin/building-a-generic-and-performant-networking-layer-in-flutter-25e2</link>
      <guid>https://dev.to/dklimkin/building-a-generic-and-performant-networking-layer-in-flutter-25e2</guid>
      <description>&lt;p&gt;&lt;strong&gt;Implement a high-performant and scalable networking layer&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;These days it’s hard to find an app that doesn’t perform any API calls. So might be yours. Luckily, Flutter provides packages out of the box to make this process a bit simpler. Examples of those could be easily found on pub.dev: &lt;a href="https://pub.dev/packages/http" rel="noopener noreferrer"&gt;HTTP&lt;/a&gt; and &lt;a href="https://pub.dev/packages/dio" rel="noopener noreferrer"&gt;DIO&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Essentially, both packages allow you to assemble a network request and then perform it. Leaving to you error handling, JSON parsing, and all other aspects of mobile application networking.&lt;/p&gt;

&lt;p&gt;For example, your API call looks like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;This code is totally fine for a small pet project. But you start noticing inconveniences when the project starts growing at the scale stage.&lt;/p&gt;

&lt;p&gt;Specifically:&lt;/p&gt;

&lt;p&gt;❌ Duplicate code&lt;br&gt;
❌ Blocking main thread and dropping FPS&lt;br&gt;
❌ Error-prone codebase&lt;br&gt;
❌ More and more time on adding new services and API calls&lt;br&gt;
❌ Unreliable error handling&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fak4ewzlxfpfcv26omvs7.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fak4ewzlxfpfcv26omvs7.gif" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Smells bad🤮. Right?&lt;/p&gt;

&lt;p&gt;Here we’re going to tackle all these issues and implement high-performant and scalable networking layer. Let’s start from the very top. Ideally, our API call should look very simple:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi9o9wb1rulm79x7amegr.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi9o9wb1rulm79x7amegr.gif" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Quite nice?&lt;/p&gt;

&lt;p&gt;Now let’s start crafting our Networking Layer step by step with a help of one amazing package: &lt;a href="https://pub.dev/packages/freezed" rel="noopener noreferrer"&gt;Freezed&lt;/a&gt; (yet another code generator for unions/pattern-matching and copy)!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 0: Design our data model. Let it be something easy to grasp&lt;/strong&gt;&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Step 1: Create a flexible request body&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Request body encapsulated data to send along with your API call and could contain JSON object, binary data, text, etc.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Step 2: Create a request&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Our request will contain common parameters you might need to serve an API call: type (Get, Post, etc), API path, data to pass as a body, optionally query parameters and headers to override globally specified headers if needed:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Step 3. Create a response&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Our response object will contain only raw data received from your API:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Now we are ready to implement the most intriguing part: network service/layer itself!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4. Let’s prototype our service with what we already have&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Our service needs a base URL and optionally DIO instance and HTTP headers. It will create a default DIO instance if not provided:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;You can add other body types and conversions here so I’m leaving it to you.&lt;/p&gt;

&lt;p&gt;Everything is ready to execute our request and handle exceptions:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;We can also add a method(s) to simplify authenticated API calls:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Let’s summarise what we’ve got here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;API call is prepared with all headers combined and parameters incapsulated&lt;/li&gt;
&lt;li&gt;API call is executed by DIO package&lt;/li&gt;
&lt;li&gt;Data returned from DIO is converted to your Model type provided&lt;/li&gt;
&lt;li&gt;Exceptions are handled and wrapped in a corresponding API response&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But all this stuff still happens on main thread. And this is something we are going to fix now by utilizing a powerful yet simple &lt;a href="https://dart.dev/guides/language/concurrency" rel="noopener noreferrer"&gt;Isolates mechanism&lt;/a&gt; in Dart.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcl071atn7t13df80xd8a.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcl071atn7t13df80xd8a.gif" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5. Make Networking Layer to be performant!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The way Dart work is organized to pass data between Isolates we need to copy all parameters and then call a globally defined function. We will encapsulate all data in a private &lt;code&gt;PreparedNetworkRequest&lt;/code&gt; class:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;And now all we need to do is to move request execution call along with exception handling into Isolate function:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;And call it in our Network Service:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbkoye6olg4t4xo26jyn2.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbkoye6olg4t4xo26jyn2.gif" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Huh! We’re done coding☺️.&lt;/p&gt;

&lt;p&gt;Finally, our &lt;code&gt;NetworkService&lt;/code&gt; looks like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Recap of what we’ve achieved today&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;✅ No duplicate code&lt;br&gt;
✅ Not blocking main thread and dropping FPS&lt;br&gt;
✅ Error-proof codebase&lt;br&gt;
✅ Minimised time on adding new services and API calls&lt;br&gt;
✅ Structured and strongly-typed error handling&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Resources&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dart.dev/guides/language/concurrency" rel="noopener noreferrer"&gt;Concurrency in Dart&lt;/a&gt;&lt;br&gt;
&lt;a href="https://pub.dev/packages/freezed" rel="noopener noreferrer"&gt;Freezed Dart package&lt;/a&gt;&lt;br&gt;
&lt;a href="https://pub.dev/packages/dio" rel="noopener noreferrer"&gt;Dio Dart package&lt;/a&gt;&lt;br&gt;
&lt;a href="https://betterprogramming.pub/modern-networking-layers-in-ios-using-async-await-ab98a4b5802c" rel="noopener noreferrer"&gt;RealHTTP Modern Networking Layers in iOS Using Async/Await&lt;/a&gt;&lt;br&gt;
&lt;a href="https://gist.github.com/dev4jam/ce094af46be6150cd0d20322e37dd958" rel="noopener noreferrer"&gt;Full source code available here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To get hand-picked the latest tech stories subscribe on my &lt;a href="https://t.me/itjedidev" rel="noopener noreferrer"&gt;Telegram Channel&lt;/a&gt; where I post daily.&lt;/p&gt;

&lt;p&gt;Happy coding!😉&lt;/p&gt;

</description>
      <category>flutter</category>
      <category>dart</category>
      <category>ios</category>
      <category>android</category>
    </item>
  </channel>
</rss>
