Getting to know Clean Architecture in Flutter

Bayu Nugroho
3 min readJul 12, 2022

what is clean architecture? Clean architecture is one of the architectural patterns popularized by Uncle Bob or Robert C. Martin. Just as European architectural styles are different from Asian architectures, until now there have also been many emerging architectures for software development. Some of them are Onion Architecture, Lean Architecture, Hexagonal Architecture, BCE, etc. Although they are different, they all have the same goal, namely the separation of interests or separation.Why do we need Architecture?

“The goal of software architecture is to minimize the human resources required to build and maintain the required system.” ― Robert C. Martin, Clean Architecture

in the sentence above what Robert C.martin said is to minimize the human resources needed to build and maintain the required system.Advantages of using Architecture

The advantages of using this architecture are:

  • Independent of framework : It does not depend on the implementation of the framework used and puts the framework only as a tool.
  • Independent of UI : The UI can be changed easily, without the need to change the whole system.
  • Independent of database : It doesn’t depend on any particular database framework and can be replaced easily.
  • Independent of external : Existing business processes don’t need to know what’s on the outside.
  • Testable : Code for business processes can be tested without requiring a UI, database, or other external components.

The figure shows the application is divided into several layers (layers). The deeper the layer, the more abstract and high-level it is. The inner layer or circle contains the business logic, while the outer layer contains the implementation.Dependency Rules

The Dependencies Rule states that source code dependencies can only point inward.

This means that nothing in the inner circle can know anything about something in the outer circle. and the inner circle must not depend on anything in the outer circle. For example, Entities do not depend on anything, whereas Use Cases depend only on Entities.

The clean architecture generally divides the application into several parts:

  • Presentation

The Presentation Layer contains the Flutter implementation for displaying the UI. and if you want to add state management like getx, provider, bloc this is the place.

  • Domain

Domain layer is the core part related to business processes. This section contains business objects (entities) and business logic (use cases).

  • Data

The data layer contains the repository implementation as well as data sources such as (database or shared preferences) or remote (API). Remember, the clean architecture has a dependency rule where the data layer depends on the domain and not the other way around. Then how to access the data screen and send the data to the UI side? The solution is to use one of the SOLID principles, namely the Dependency Inversion Principle. We can create an interface or an abstract class and then create both classes depending on it.

Conclusion

A good architecture is the key to building applications that are modular, scalable, maintainable and testable. Clean Architecture is the core architecture for many applications today, mainly because it allows to properly separate business logic and scale.

A clean architecture does require more code to implement, so implementing a clean architecture for a small project may be overkill. But, it reduces the complexity and it is necessary for large scale projects. Keep in mind that each project may have its own requirements, so this article does not cover the exact and perfect style of the project.

Hope this is helpful and you learn something new. Thank you for reading this article✨

--

--

Bayu Nugroho

Passionate programmer obsessed with perfection. Enjoys creating web and mobile applications, exploring music, technical blogging, and more.