Flutter Clean Architecture

Isaias Cuvula
5 min readAug 6, 2023
Photo by Lance Anderson on Unsplash

In the world of mobile app development, building clean and maintainable code is crucial for long-term success. This is where Clean Architecture comes into play. Clean Architecture is a software design pattern that helps separate the concerns of different layers in an application, making it easier to understand, test, and modify. In this article, we will explore what Flutter Clean Architecture is, its benefits, and how to implement it in your Flutter projects.

What is Clean Architecture?

Clean Architecture is a software design pattern introduced by Robert C. Martin, also known as Uncle Bob. It promotes the separation of concerns and the dependency inversion principle, allowing for highly modular and testable code. The main idea behind Clean Architecture is to separate the business logic from the external dependencies, such as frameworks or databases.

Why use Clean Architecture in Flutter?

Flutter Clean Architecture is an architecture pattern that focuses on the separation of concerns, modularity, and maintainability.

Implementing Clean Architecture in your Flutter projects offers several benefits. Here are a few:

  1. Modularity: Clean Architecture helps you break down your code into isolated and independent modules, making it easier to understand and maintain. Each module has a clear responsibility, which leads to better organization and scalability.
  2. Testability: By separating the business logic from the external dependencies, Clean Architecture allows for easy unit testing. You can test the core functionality of your app without worrying about the UI or other external factors.
  3. Flexibility: Clean Architecture makes it easier to modify or replace external dependencies. If you decide to switch to a different database or UI framework, you can do so without affecting the core business logic. This flexibility is essential for keeping your app up-to-date with the latest technologies.
  4. Code Reusability: With Clean Architecture, you can reuse the core business logic across multiple platforms. This means that if you decide to build a web or desktop version of your app in the future, you can leverage the same codebase and logic.

Implementing Clean Architecture in Flutter

To implement Clean Architecture in your Flutter project, you can follow these general steps:

  1. Identify the Layers: Identify the different layers in your application, such as presentation, domain, and data layers. The presentation layer handles the UI and user interaction, the domain layer contains the core business logic, and the data layer deals with external data sources.
  2. Define Dependencies: Define the dependencies between the layers. The domain layer should not depend on the data layer or the presentation layer. Instead, both the data and presentation layers should depend on the domain layer. This ensures that the core business logic remains independent of the implementation details.
  3. Implement Use Cases: Implement use cases in the domain layer. Use cases encapsulate the business logic and define the operations that can be performed in your application. Each use case should have a clear input and output, making it easy to test and understand.
  4. Use Dependency Injection: Use a dependency injection framework, such as: riverpod, get_it, or ioc_container, to manage the dependencies between the layers. This allows you to easily swap out implementations and provides better testability.
  5. Separate UI and Business Logic: Keep the UI code separate from the business logic. Use Flutter’s widget composition to build reusable UI components and keep the business logic in the domain layer.
  6. Write Unit Tests: Write unit tests for the business logic in the domain layer. This ensures that the core functionality of your app is working as expected and makes it easier to catch bugs or regressions.

By following these steps, you can implement Clean Architecture in your Flutter projects and enjoy the benefits of clean and maintainable code.

Here is a basic structure for Flutter Clean Architecture that emphasizes scalability and testability:


lib/
|-- config/
| |-- routes/
| |-- theme/
|-- data/
| |-- datasources/
| | |-- remote/
| | | |-- user/
| | | | |-- user_remote_data_source.dart
| | | | |-- user_remote_data_source_impl.dart
| | | |-- post/
| | | | |-- post_remote_data_source.dart
| | | | |-- post_remote_data_source_impl.dart
| | | |-- comment/
| | | | |-- comment_remote_data_source.dart
| | | | |-- comment_remote_data_source_impl.dart
| | |-- local/
| |-- repositories/
| |-- models/
|-- domain/
| |-- entities/
| |-- repositories/
| |-- usecases/
|-- presentation/
| |-- pages/
| |-- widgets/
| |-- blocs/providers
| |-- utils/
|-- di/
| |-- injection_container.dart
|-- main.dart

Let’s break down each folder and its contents:

data/: This folder contains all the implementation details of the application's data layer. It is further divided into:

  • datasources/: This folder contains the implementation of data sources, which can be either remote or local. For example, an API client to communicate with a backend server can be a remote data source, while a local database can be a local data source.
  • repositories/: This folder contains the implementation of repositories, which act as a single source of truth for the data. Repositories provide an abstraction over the data sources and handle data retrieval and storage.
  • models/: This folder contains the implementation of data models, which are used to represent the data entities of the application.

domain/: This folder contains the implementation of the application's domain layer. The Domain layer will contain our core business rules (entity) and application-specific business rules (use cases). It is further divided into:

  • entities/: This folder contains the implementation of domain entities, which represent the real-world objects of the application. Entities should be independent of any specific implementation detail.
  • repositories/: This folder contains the interfaces of repositories. Interfaces define the contracts that the repositories must adhere to.
  • usecases/: This folder contains the implementation of use cases, which define the business logic of the application. Use cases depend on the domain entities and the repository interfaces.

presentation/: This folder contains the implementation of the application's presentation layer. It is further divided into:

  • pages/: This folder contains the implementation of the pages or screens of the application.
  • widgets/: This folder contains the implementation of reusable widgets that are used across different pages or screens.
  • blocs/providers: This folder contains the implementation of BLoCs (Business Logic Components) or providers, which are responsible for managing the state of the application. BLoCs/providers depend on the use cases and provide the necessary data to the pages or screens.
  • utils/: This folder contains helper classes or functions that are used across the presentation layer.

injection_container.dart: This file contains the dependency injection setup for the application. It defines how the different components of the application should be created and wired together.

main.dart: This is the entry point of the application.

By following this structure, you can ensure that your codebase is scalable, testable, and maintainable. Additionally, you can apply various design patterns and principles such as SOLID, Dependency Injection, and Test-Driven Development (TDD) to further improve the quality of your codebase.

Flutter clean architecture folder structure

Flutter Clean Architecture is a powerful software design pattern that promotes modularity, testability, flexibility, and code reusability. By separating the concerns of different layers and following the principles of Clean Architecture, you can build robust and scalable Flutter applications. So give Clean Architecture a try in your next Flutter project and experience the benefits firsthand! Happy Coding!

--

--