Home / Architecture

My architecture

// How I structure a Flutter app so it scales, can be tested and stays easy to maintain over time.

Why Clean Architecture

01

Separation by layers

Each responsibility lives in its layer. The interface doesn't know where the data comes from and the data doesn't know how it will be painted.

02

Dependencies towards the domain

Presentation and Data depend on the domain, never the other way around. The domain is pure Dart: it knows nothing about Flutter, the network or storage.

03

Cache-first

Repositories look at local data first and only go to the network when nothing is stored. The app responds fast and works without a connection.

04

Everything can be tested

Each piece receives its dependencies through the constructor. That way use cases, repositories, mappers and BLoCs are tested in isolation.

Four layers, one responsibility each

An app is divided into four layers with strict dependency rules. The arrows show who knows whom: everything points to the domain and the core layer supports the rest.

Presentation BLoC · Cubit · pages · widgets
Domain entities · use cases · contracts
Data datasources · DTO/DBO · mappers · repositories
↑ Core supports all layers ↑
Core services · dependency injection · network · navigation · errors · configuration
Core lib/core

Shared infrastructure

Everything the whole app uses: network services, navigation, connectivity and logging, dependency injection, per-environment configuration and centralized error handling. It imports nothing from the other layers.

Domain lib/layers/domain

Business rules

Pure Dart. Immutable entities, repository contracts and one use case per business action. It is the most stable layer because it depends on no other, neither Flutter nor external packages.

Data lib/layers/data

Data access

It talks to the outside world. Remote and local datasources that return DTOs and DBOs, mappers that turn them into entities, and repositories that decide where each piece of data comes from. DTOs never leave this layer.

Presentation lib/layers/presentation

User interface

Each screen is a feature with its BLoC or Cubit, its page and its widgets. The view only paints states and fires events. Navigation, dialogs and notices are also handled from the BLoC.

From the tap to the screen

This is how a piece of data travels from the moment the user taps the screen until they see the result. Each step has a single responsibility.

Presentation Step 1 of 6

UI event

The user interacts and the view sends an event to the BLoC.

Presentation Step 2 of 6

BLoC

It emits a loading state and delegates to the use case. It contains no business logic.

Domain Step 3 of 6

Use case

It represents a single business action and requests the data from the repository through its contract.

Data Step 4 of 6

Repository

It applies cache-first: it looks locally and, if there is no data, requests it remotely, saves it and turns it into entities with its mapper.

Data Step 5 of 6

Datasource

It is the only piece that calls the API or storage. It returns DTOs or DBOs, never entities.

Presentation Step 6 of 6

Back to the UI

The entity goes up to the BLoC, which emits success or error, and the view repaints according to the new state.

BLoC or Cubit, depending on the case

BLoC Full screens

When there are several events

For complete features with several distinct events or events that carry data. Events are named in the past tense, states are nouns and a single handler dispatches each event.

Cubit Isolated components

When a single state change is enough

For small, independent pieces such as a clock, a calendar, a switch or some tabs. Simple state changes with no need for events: a method emits the new state.

Every asynchronous operation follows the same cycle: in progress → success or error. The view decides what to paint with a switch expression on the state, with no if/else chains.

Day-to-day rules

  • Immutable entities: fields final and copyWith, with no logic or serialization.
  • DTOs and DBOs don't leave the data layer: only entities travel upward.
  • No API calls outside the datasources.
  • No business logic in the interface or the BLoC: that's what use cases are for.
  • Navigation, dialogs and snackbars are triggered from the BLoC, never from a widget.
  • One class per file: one entity, DTO or widget per file.
  • Before creating a new widget, I reuse or extend an existing one.
  • Folders in kebab-case, files in snake_case and classes with a type suffix (UserEntity, UserRepositoryImpl).
  • Strict static analysis and 120-column formatting from the first commit.
  • A versioned CHANGELOG updated with every delivery.

Tools for every project

Flutter Dart BLoC Cubit Sealed classes GetIt injectable Dio easy_localization
flutter_secure_storage shared_preferences Hive SQLite Firebase Messaging mockito bloc_test

Want this architecture in your app?