Sub topic 1
Why Structure Matters as an App Grows
Prerequisites
This chapter assumes that you can build Flutter screens with widgets, use Dart imports and exports, and navigate between pages. You should also understand the basic idea of a repository: an object that hides where data comes from and provides operations such as loading or saving it.
The cost of a small app's structure
A new Flutter app can work well with a simple folder layout:
lib/
screens/
widgets/
models/
services/This layout is layer-first. Files are grouped by their technical role. All screens go in one folder, all models in another, and all services in a third.
Layer-first organization is easy to understand at the beginning. The problem appears when the app grows. A feature such as checkout may need a page, several widgets, models, API clients, validation rules, and state-management code. Those files become scattered across many folders. Finding one behavior requires searching by name across the whole project.
A change can also affect unrelated features. A developer editing a payment model may accidentally change code used by the catalog. The folder layout does not show which files belong together or which dependencies are safe.
Feature-first organization
Feature-first organization groups code by the user capability it supports. A catalog feature owns catalog code. A cart feature owns cart code. Shared infrastructure remains outside those features.
lib/
app/
core/
features/
catalog/
cart/
checkout/A feature folder can still contain layers. The difference is that the layers are local to the feature:
features/cart/
data/
domain/
presentation/This structure keeps related decisions close together. A developer working on the cart can inspect its screen, business rules, and data implementation without opening unrelated folders. It also makes ownership clearer. If a file is needed only by checkout, it belongs in checkout rather than in a general-purpose folder.
Structure reduces accidental coupling
Coupling is the amount of knowledge one part of a system has about another part. High coupling makes changes risky because one feature depends on another feature's private details.
A feature-first layout does not automatically prevent coupling. It gives you a place to define boundaries. For example, the cart can expose a small interface for its item count. Checkout can use that interface without importing the cart's widgets or database classes.
Good boundaries provide several benefits:
- A feature can change its internal implementation without changing every caller.
- Tests can replace real data sources with fakes.
- Developers can find the code responsible for a behavior quickly.
- Teams can work on different features with fewer merge conflicts.
- Removing or replacing a feature becomes more manageable.
Choosing the structure deliberately
Feature-first structure is useful when the app has multiple business capabilities, several developers, or frequent changes in particular areas. It may be unnecessary for a small prototype with only a few screens. The goal is not to create the most folders possible. The goal is to keep change localized and dependencies understandable.
As the app grows, ask these questions:
- Can you find all code for one user capability quickly?
- Does changing one feature require editing unrelated features?
- Are shared utilities truly generic, or do they belong to one feature?
- Can a new developer discover the dependency direction from the folders and imports?
If the answers become unclear, the current structure is creating maintenance cost.
Exercise
Why can a layer-first layout become difficult to maintain in a large Flutter app?
Show answer
Code for one feature is scattered across technical folders, so behavior is harder to find and changes can create accidental dependencies on unrelated features.