<?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: Nguyễn Minh Đức</title>
    <description>The latest articles on DEV Community by Nguyễn Minh Đức (@ngmduc2012).</description>
    <link>https://dev.to/ngmduc2012</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%2F929143%2Fcae1e7c3-2a98-421c-94e4-957986e507e6.jpg</url>
      <title>DEV Community: Nguyễn Minh Đức</title>
      <link>https://dev.to/ngmduc2012</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ngmduc2012"/>
    <language>en</language>
    <item>
      <title>Maintain and Extend Code Easily with Clean Architecture for Flutter</title>
      <dc:creator>Nguyễn Minh Đức</dc:creator>
      <pubDate>Fri, 07 Feb 2025 01:56:57 +0000</pubDate>
      <link>https://dev.to/ngmduc2012/maintain-and-extend-code-easily-with-clean-architecture-for-flutter-2apm</link>
      <guid>https://dev.to/ngmduc2012/maintain-and-extend-code-easily-with-clean-architecture-for-flutter-2apm</guid>
      <description>&lt;p&gt;&lt;strong&gt;Maintain and Extend Code Easily with Clean Architecture&lt;/strong&gt;  &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;1. Common Issues in Application Development&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;When developing software, especially large-scale projects, you may face the following issues:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Maintenance difficulties&lt;/strong&gt;: Fixing bugs and maintaining functionality can become time-consuming as the application grows.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Adding new features&lt;/strong&gt;: Clients may request additional features that need to be integrated without impacting existing functionality.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scaling the project&lt;/strong&gt;: Large projects with limited resources require a well-organized code structure for efficient development.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Improved collaboration&lt;/strong&gt;: A clear and structured codebase makes it easier for team members to collaborate and transition responsibilities.
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;2. Clean Architecture&lt;/strong&gt;
&lt;/h3&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Key Benefits&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Clean Architecture&lt;/strong&gt; provides a systematic way to organize code, offering benefits such as:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Increased flexibility&lt;/strong&gt;: The most changeable components are placed at the outer layer, simplifying API updates or data handling during maintenance.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Efficient module management&lt;/strong&gt;: Each module is designed to be independent, making it easier to maintain and extend.
&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Architecture Structure&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Domain (Core)&lt;/strong&gt;: The innermost layer containing the business logic, which changes infrequently.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Presentation (UI)&lt;/strong&gt;: The middle layer responsible for handling data from the domain and rendering it to the user interface.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Data&lt;/strong&gt;: The outermost layer responsible for API communication or data storage.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Main principles&lt;/strong&gt;:  &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Inner layers must not depend on outer layers&lt;/strong&gt;: Domain should not directly access Presentation or Data layers.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Outer layers can interact with inner layers&lt;/strong&gt;: Data can call Domain or Presentation as needed.
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;3. Implementing Clean Architecture in Flutter&lt;/strong&gt;
&lt;/h3&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;3.1. Folder Structure&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fvhbbjuew1lvzrmhlnqaj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fvhbbjuew1lvzrmhlnqaj.png" alt="Folder Structure" width="304" height="491"&gt;&lt;/a&gt;&lt;br&gt;
A Flutter project is typically organized as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;lib/
  ├── features/          # Contains the main modules
  │     ├── module_1/    # Each module includes three layers: Data, Domain, Presentation
  │     └── module_2/
  ├── ...
  └── main.dart          # Application entry point
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;3.2. Automating Structure Creation&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Manually creating folders and files can be time-consuming, especially when setting up a new module. You can use &lt;strong&gt;Mason&lt;/strong&gt; for automation:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Mason&lt;/strong&gt; helps generate Clean Architecture structures quickly.
&lt;/li&gt;
&lt;li&gt;Refer to the tool: &lt;a href="https://wongcoupon.com/en/doc/help/flutter/boost-your-flutter-development-efficiency-with-mason" rel="noopener noreferrer"&gt;Boost your Flutter development efficiency with Mason&lt;/a&gt;.
&lt;/li&gt;
&lt;li&gt;Check out the template: &lt;a href="https://brickhub.dev/bricks/dr_clean_arch" rel="noopener noreferrer"&gt;dr_clean_arch on Brickhub&lt;/a&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;3.3. Layer Details&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Frm63sm4uax1ew3olks1s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Frm63sm4uax1ew3olks1s.png" alt="Layer Details" width="470" height="631"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h5&gt;
  
  
  &lt;strong&gt;Domain Layer&lt;/strong&gt;
&lt;/h5&gt;

&lt;p&gt;This is the innermost layer, containing the business logic. It usually consists of:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Entities&lt;/strong&gt;: Core objects that represent data models. Examples include plugins like &lt;a href="https://brickhub.dev/bricks/dr_entity" rel="noopener noreferrer"&gt;dr_entity&lt;/a&gt; using &lt;code&gt;json_serializable&lt;/code&gt; and &lt;a href="https://brickhub.dev/bricks/dr_freezed" rel="noopener noreferrer"&gt;dr_freezed&lt;/a&gt;.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use Cases&lt;/strong&gt;: Business logic functions. These highlight how data flows in and out of the data layer, making diagrams or workflows easier to manage.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Enums&lt;/strong&gt;: Enums simplify state management. Check out templates like &lt;a href="https://brickhub.dev/bricks/dr_enum" rel="noopener noreferrer"&gt;dr_enum&lt;/a&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  &lt;strong&gt;Presentation Layer&lt;/strong&gt;
&lt;/h5&gt;

&lt;p&gt;This layer manages UI rendering and user interactions. It typically includes:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Views&lt;/strong&gt;: Screens and their components.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;View Models&lt;/strong&gt;: State management solutions using &lt;a href="https://pub.dev/packages/get" rel="noopener noreferrer"&gt;GetX&lt;/a&gt;, &lt;a href="https://bloclibrary.dev/" rel="noopener noreferrer"&gt;BLoC&lt;/a&gt;, or &lt;a href="https://riverpod.dev/" rel="noopener noreferrer"&gt;Riverpod&lt;/a&gt;.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Widgets&lt;/strong&gt;: Reusable UI components. For reusable widgets across projects, consider placing them in packages.
&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  &lt;strong&gt;Data Layer&lt;/strong&gt;
&lt;/h5&gt;

&lt;p&gt;This layer handles APIs or data storage. It includes:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Constants&lt;/strong&gt;: Module-specific constants.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Repositories&lt;/strong&gt;: Manage API calls and local data handling.
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;4. Implementing Clean Architecture for the PAYMENT Module&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fhd38pjciaoracz0v0fqz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fhd38pjciaoracz0v0fqz.png" alt="Implementing" width="443" height="442"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;PAYMENT Module Structure&lt;/strong&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;lib/features/payment/
  ├── data/
  │     ├── repositories/    # Repository for API handling
  │     └── constants/       # Constants related to payments
  ├── domain/
  │     ├── entities/        # Entities for data processing
  │     ├── usecases/        # Core use cases (fetch bill, process payments)
  │     └── enums/           # Enums for payment status
  └── presentation/
        ├── views/           # UI screens
        ├── view_models/     # State management with BLoC/GetX
        └── widgets/         # Reusable UI components
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;Component Details&lt;/strong&gt;
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Domain&lt;/strong&gt;:&lt;br&gt;&lt;br&gt;
&lt;a href="https://media2.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%2Fpu0irsp85ycdn6ff5gzt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fpu0irsp85ycdn6ff5gzt.png" alt="Domain" width="477" height="836"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Entities&lt;/strong&gt;: Represent invoice and customer objects.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use Cases&lt;/strong&gt;: Functionalities like payment validation and fetching invoice lists.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Enums&lt;/strong&gt;: Payment states (pending, success, failure).
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Presentation&lt;/strong&gt;:&lt;br&gt;&lt;br&gt;
&lt;a href="https://media2.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%2Fsz5hn6n9s9ygk9btt27a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fsz5hn6n9s9ygk9btt27a.png" alt="Presentation" width="456" height="613"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Views&lt;/strong&gt;: Display payment UI.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;View Models&lt;/strong&gt;: State management with BLoC.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Widgets&lt;/strong&gt;: Payment-related buttons and forms.
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Data&lt;/strong&gt;:&lt;br&gt;&lt;br&gt;
&lt;a href="https://media2.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%2Fthwplsmj2vqlbt3ohop1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fthwplsmj2vqlbt3ohop1.png" alt="Data" width="359" height="310"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Repositories&lt;/strong&gt;: Handle payment APIs or offline data.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Constants&lt;/strong&gt;: API endpoint configurations.
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;5. References&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html" rel="noopener noreferrer"&gt;Learn more about Clean Architecture&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://pub.dev/packages/flutter_clean_architecture#usage" rel="noopener noreferrer"&gt;Flutter Clean Architecture Usage&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://medium.com/@yamen.abd98/clean-architecture-in-flutter-mvvm-bloc-dio-79b1615530e1" rel="noopener noreferrer"&gt;Clean Architecture with BLoC&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://wongcoupon.com/en/doc/help/flutter/maintain-and-extend-code-easier-with-clean-architecture" rel="noopener noreferrer"&gt;My article&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you found this guide helpful, consider supporting my work by &lt;a href="https://buymeacoffee.com/ducmng12g" rel="noopener noreferrer"&gt;buying me a coffee&lt;/a&gt;! ☕&lt;/p&gt;

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