<?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: DUBYDU</title>
    <description>The latest articles on DEV Community by DUBYDU (@dubydu).</description>
    <link>https://dev.to/dubydu</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%2F884813%2Fc39079aa-d0cc-4d95-bc9d-c8471208da16.jpeg</url>
      <title>DEV Community: DUBYDU</title>
      <link>https://dev.to/dubydu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dubydu"/>
    <language>en</language>
    <item>
      <title>Flutter x Clean Architecture</title>
      <dc:creator>DUBYDU</dc:creator>
      <pubDate>Thu, 30 Jun 2022 01:50:42 +0000</pubDate>
      <link>https://dev.to/dubydu/flutter-x-clean-architecture-841</link>
      <guid>https://dev.to/dubydu/flutter-x-clean-architecture-841</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;In the scope of this article, I assume that you already know what Clean Architecture is, and why should we use it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  ./ Flutter Clean Architecture
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mWxhWvIY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/68zk6fn6u38o7wzquncb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mWxhWvIY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/68zk6fn6u38o7wzquncb.png" alt="Image description" width="880" height="391"&gt;&lt;/a&gt;&lt;br&gt;
As you can see, there are three main layers: &lt;strong&gt;Presentation, Domain, and Data&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Role and description of each layer:&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  I. Presentation
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;There is no business logic processing on this layer, hence it is only used to show UI and handle events.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Widget (Page)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Notify BloC of events such as screen display and user touch events, and Listen to the events emitted from BloC as well.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. BloC&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Receive events from View and execute UseCase according to the event if necessary.&lt;/li&gt;
&lt;li&gt;Emit the data received from UseCase to View.&lt;/li&gt;
&lt;li&gt;BloC doesn’t know about View at all.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  II. Domain
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;This layer is responsible for business logic.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. UseCase&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Describes the logic processing required for the UseCase.&lt;/li&gt;
&lt;li&gt;Work directly to Repository.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. Translator&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Convert the model into the entity in order to request to server-side, or convert the entity into the model to use for Presentation layer.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;3. Model&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A model will not depend on the data that acquire from server-side.&lt;/li&gt;
&lt;li&gt;A model is used for the Presentation layer.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  III. Data
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;This layer is responsible for communicating with server-side and data management logic.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. DataSource&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Describes the process to acquire and update the data.&lt;/li&gt;
&lt;li&gt;This is the place decided whether to get the data from the server-side or use the data in the DB or cache.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. Entity&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An entity will depend on the data that acquire from server-side.&lt;/li&gt;
&lt;li&gt;An entity is not used for the Presentation layer.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;3. Repository&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The bridge between Data layer and Domain layer.&lt;/li&gt;
&lt;li&gt;We will use Translator in Repository in order to convert data.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  ./ Practice
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;For a better understanding, I’d like to demonstrate a flow that receives an action from Widget, processes it, requests/receives data from the server-side, and finally displays the data to Widget.&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Source-code: &lt;a href="https://github.com/dubydu/fluttourII"&gt;https://github.com/dubydu/fluttourII&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;1. Data Layer&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create an &lt;strong&gt;Entity&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;


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




&lt;ul&gt;
&lt;li&gt;Inside &lt;a href="https://github.com/dubydu/fluttourII/blob/master/lib/data/api/api_client/api_client_type.dart"&gt;api_client_type.dart&lt;/a&gt;, define an HTTP request like this:&lt;/li&gt;
&lt;/ul&gt;


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




&lt;ul&gt;
&lt;li&gt;Then, open your terminal, and run this command: &lt;code&gt;make gen&lt;/code&gt;, the &lt;a href="https://pub.dev/packages/retrofit"&gt;retrofit&lt;/a&gt; will take the rest responsibility. You also can custom the command line by editing this &lt;a href="https://github.com/dubydu/fluttourII/blob/master/Makefile"&gt;Makefile&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;Define &lt;strong&gt;DataSource&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;


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



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




&lt;ul&gt;
&lt;li&gt;Implement &lt;strong&gt;Repository&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;


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



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




&lt;p&gt;&lt;strong&gt;2. Domain Layer&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create &lt;strong&gt;Model&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;


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




&lt;ul&gt;
&lt;li&gt;Define &lt;strong&gt;Translator&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;


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




&lt;ul&gt;
&lt;li&gt;Implement &lt;strong&gt;UseCase&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;


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



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




&lt;p&gt;&lt;strong&gt;3. Presentation Layer&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Define &lt;strong&gt;BloC&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;


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




&lt;ul&gt;
&lt;li&gt;Implement &lt;strong&gt;Widget&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;


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




&lt;p&gt;&lt;strong&gt;4. DI&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Let’s make our code becomes more modular. 😎&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Create &lt;strong&gt;config_module&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;


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




&lt;ul&gt;
&lt;li&gt;Create &lt;strong&gt;client_module&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;


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




&lt;ul&gt;
&lt;li&gt;Create &lt;strong&gt;datasource_module&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;


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




&lt;ul&gt;
&lt;li&gt;Create &lt;strong&gt;repository_module&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;


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




&lt;ul&gt;
&lt;li&gt;Create &lt;strong&gt;usecase_module&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;


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




&lt;ul&gt;
&lt;li&gt;Inject UseCase to BloC in &lt;a href="https://github.com/dubydu/fluttourII/blob/master/lib/my_app.dart"&gt;my_app.dart&lt;/a&gt; by conforming to all of the modules.&lt;/li&gt;
&lt;/ul&gt;


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


&lt;p&gt;That’s all, run the project, and let’s see what happens!&lt;/p&gt;



&lt;h3&gt;
  
  
  Pitfalls
&lt;/h3&gt;

&lt;p&gt;In order to run this project, you need to run in a specific environment.&lt;/p&gt;

&lt;p&gt;Development: &lt;code&gt;flutter run -t lib/main_dev.dart&lt;/code&gt;&lt;br&gt;
Production: &lt;code&gt;flutter run -t lib/main_prod.dart&lt;/code&gt;&lt;/p&gt;

</description>
      <category>flutter</category>
      <category>dart</category>
      <category>cleanarchitecture</category>
    </item>
  </channel>
</rss>
