Clean Architecture in Flutter

Utsav Ghimire
3 min readAug 27, 2022

--

You are here means you don’t need any explanation about clean architecture and why we need it.

Image source: Internet

Good architecture is key to building a modular, scalable, maintainable, and testable application. Clean Architecture is a core architecture for many applications mainly because it separates business logic from the UI and scales well.

There are three main layers in Clean Architecture. They are Presentation, Domain, and Data layer.

Presentation

The presentation layer is the most framework-dependent layer. It is responsible for all the UI and handling the events in the UI. It does not contain any business logic.

a. Widget (Screens/Views)

  • Widgets notify the events and listen to the states emitted from the Bloc.

b. BloC

  • Receive the events from the views and interact with use cases accordingly.
  • Emit the state according to the data received from the use cases.

Bloc does not know about the view at all. It communicates in the form of states and events only.

Domain

The domain layer is responsible for all the business logic. It is written purely in Dart without flutter elements because the domain should only be concerned with the business logic of the application, not with the implementation details.

a. Use Cases

  • Describes the logic processing required for the application.
  • Communicates directly with the repositories.

b. Repositories

  • Abstract classes that define the expected functionality of outer layers.

c. Entities

  • Business objects of the application.
  • An entity will not depend on the data acquired from the server side.

Data

The data layer is the outermost layer of the application and is responsible for communicating with the server-side or a local database and data management logic. It also contains repository implementations.

1. DataSource

  • Describes the process of acquiring and updating the data.
  • Consist of remote and local Data Sources. Remote Data Source will perform HTTP requests on the API. At the same time, local Data sources will cache or persist data.

2. Models

  • An entity will depend on the data acquired from the different data sources.
  • Serialization of JSON structure into Dart maps Entity objects to Models and vice-versa.

3. Repository

  • The bridge between the Data layer and the Domain layer.
  • Actual implementations of the repositories in the Domain layer. Repositories are responsible for coordinating data from the different Data Sources.

Service Locator

In this implementation of Clean Architecture, we are using the Service Locator pattern. All the use cases and implementations are injected and extracted through the service locators.

Want to know more about dependency injection?

Functional Programming

Clean Architecture should not be full of surprises, so we are using functional programming in our implementation.

Buy me a coffee

https://www.buymeacoffee.com/theutsavg

https://www.buymeacoffee.com/theutsavg

Code

Connect with me

--

--