Sub topic 2
Layer-First Versus Feature-First
Assume you can build Flutter screens with widgets and navigation. You should also know Dart imports, export, and the basic idea of repositories and state management. This chapter compares two ways to arrange the files in a growing Flutter app.
Layer-first organization
A layer-first project groups files by technical role. All widgets go in one folder, all repositories go in another, and all state classes go in a third.
lib/
screens/
widgets/
models/
repositories/
services/
state/This structure is easy to understand in a small application. A developer looking for a repository knows where to look. Shared technical patterns can also be consistent because similar code sits together.
The problem appears when the application gains features. A product catalog might use files from screens, models, repositories, and state. A cart might use files from the same folders. The cart's code is spread across the project, so changing one feature requires searching several directories.
A layer-first structure also encourages broad dependencies. A screen may import a repository, a model, and a service from unrelated parts of the application. The folder names describe what a file does, but they do not show which product capability owns it.
Feature-first organization
A feature-first project groups files by user-facing capability. Each feature owns the code needed to implement that capability.
lib/
app/
core/
features/
catalog/
data/
domain/
presentation/
cart/
data/
domain/
presentation/Here, the catalog and cart code are close together. A developer can inspect one feature without navigating through every technical layer in the application.
The folders inside a feature still express layers:
presentationcontains pages, widgets, and feature-specific state.domaincontains business concepts and interfaces.datacontains repository implementations, data sources, and mappings.
Feature-first does not eliminate layers. It places the layers inside the boundary that owns them.
Comparing the two structures
| Concern | Layer-first | Feature-first |
|---|---|---|
| Finding all code for one feature | Requires several folders | Usually one feature folder |
| Reusing technical patterns | Often straightforward | Still possible through shared code |
| Risk of unrelated imports | Higher as the app grows | Lower when boundaries are enforced |
| Moving a feature to a package | Usually more work | Usually more contained |
The right choice depends on project size and team needs. Layer-first can be practical for a prototype or a small app with a few screens. Feature-first becomes useful when several developers work on independent capabilities, when features change at different rates, or when you may extract a feature into a package later.
Common mistakes
A feature-first layout can still fail if every feature imports another feature's private files. It can also fail if developers create a large common folder for code that belongs to only one capability. The directory layout helps, but dependency rules and clear public entry points complete the design.
When migrating a layer-first app, start with one cohesive feature. Move its presentation, domain, data, and tests together. Leave stable shared infrastructure in place. Repeat the process only after the first migration works.
Exercise
When is a feature-first structure a better choice than a layer-first structure?
Show answer
Choose feature-first when the app has several independently changing capabilities, because each capability's presentation, domain, and data code stay together and are easier to isolate.