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.
// How I structure a Flutter app so it scales, can be tested and stays easy to maintain over time.
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.
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.
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.
Each piece receives its dependencies through the constructor. That way use cases, repositories, mappers and BLoCs are tested in isolation.
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.
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.
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.
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.
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.
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.
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.
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.
final and copyWith, with no logic or serialization.UserEntity, UserRepositoryImpl).