Sub topic 2
Layer-first versus feature-first
This chapter assumes you can build a Flutter app with widgets and navigation, and that you know how Dart imports and export work. It also assumes you have used one state management approach and know the repository pattern at a basic level. The chapter compares the two common ways to lay out lib/ and shows when each one fits. Later chapters cover feature internals, dependency rules, and migration in detail.
Two ways to cut the same code
Every Flutter app has the same kinds of code: screens, widgets, models, repositories, state holders, and API clients. The layout question is which of two axes comes first in the folder tree.
- Layer-first groups files by technical role. The top-level folders are
models/,screens/,repositories/, and so on. - Feature-first groups files by the product capability they serve. The top-level folders are
catalog/,cart/,checkout/, and so on. Roles appear as subfolders inside each feature.
Both layouts hold the same files. Only the nesting order differs.
A layer-first tree
lib/
main.dart
models/
product.dart
cart_item.dart
order.dart
repositories/
product_repository.dart
cart_repository.dart
order_repository.dart
providers/
product_provider.dart
cart_provider.dart
order_provider.dart
screens/
catalog_screen.dart
product_detail_screen.dart
cart_screen.dart
checkout_screen.dart
widgets/
product_tile.dart
cart_row.dart
price_label.dartA feature-first tree
lib/
main.dart
app/
app.dart
router.dart
core/
network/
theme/
widgets/
price_label.dart
features/
catalog/
data/
domain/
presentation/
cart/
data/
domain/
presentation/
checkout/
data/
domain/
presentation/The layer-first tree is easy to read at a glance for a small app. The trouble starts when you ask what cart_item.dart is related to. In the layer-first tree, the answer is spread across five sibling folders. In the feature-first tree, it is one folder.
Why layer-first works early and stops working later
Layer-first is the default in many tutorials, and for good reasons. With ten screens, every folder holds a handful of files. You can scan a whole folder without scrolling. The structure also matches how most people first learn Flutter: widgets here, models there.
Four problems appear as the app grows.
- Folders get long and unrelated. A
widgets/folder with 80 files mixescart_row.dartwithprofile_avatar.dart. The folder name tells you nothing about which files belong together. - One change touches many folders. Adding a coupon field to the cart means editing a model, a repository, a provider, a screen, and a widget. Those files sit far apart in the tree. In code review, the diff is scattered, and it is hard to see which part of the product changed.
- Ownership is unclear. When two teams work on the app, each team's files are interleaved in every folder. Merge conflicts happen in shared folders, and nobody owns
providers/. - Nothing can be deleted cleanly. Removing a feature means hunting through every layer folder for its files. People often leave dead code behind because they cannot be sure it is unused.
What feature-first changes
Feature-first applies a simple rule: files that change together live together. A feature folder is the unit you reason about, review, test, and delete.
Locality of change
Most product work is described in feature terms: "improve the cart", "add search to the catalog". A feature-first tree makes that sentence map to one directory. Reviewers see one area of the diff, and the author finds the code without searching.
Imports show coupling
In a layer-first project, a cart screen imports from many top-level folders:
// lib/screens/cart_screen.dart (layer-first)
import '../models/cart_item.dart';
import '../models/product.dart';
import '../providers/cart_provider.dart';
import '../providers/product_provider.dart';
import '../widgets/cart_row.dart';
import '../widgets/price_label.dart';The same file in a feature-first project mostly uses short relative paths inside its own feature:
// lib/features/cart/presentation/cart_page.dart (feature-first)
import '../domain/cart_item.dart';
import '../../../core/widgets/price_label.dart';
import 'cart_row.dart';The second import list is shorter, and every line is informative. A path beginning with ../../../core/ means shared code. A path into another feature, for example ../../catalog/..., is a signal worth questioning. You can see coupling directly in the import lines, which is the property that later chapters turn into enforceable rules.
Clear ownership and clean deletion
A feature folder can be assigned to a team in a CODEOWNERS file with one line. If the product drops a feature, you delete its folder, then fix the compile errors in the app shell where it was registered. The compiler finds the leftovers.
What feature-first does not fix
Feature-first is a convention, not a guarantee. These are the common ways it goes wrong.
- Feature-first folders with layer-first thinking. If every feature imports the others' internals freely, you have only moved the tangle. The folder names are different, but the coupling is the same. Dependency rules, covered later, are what make the structure real.
- A
shared/folder that becomes a dumping ground. Teams put anything that is used twice into shared code. Over time it grows into a second layer-first tree. Shared code should be small and generic, and the next chapters cover the criteria. - Features that are too small or too large. A
login_buttonfeature is a widget, not a feature. A feature namedaccountthat holds profile, billing, and notifications is three features. A useful test is whether a product owner would describe it as one capability. - Duplicated models. Two features may each need a
Product. Sharing a model couples them, while duplicating it costs some code. You must decide deliberately, and the answer depends on how stable the model is.
Choosing between them
The choice is not purely ideological. Use the table as a starting point.
| Situation | Better fit |
|---|---|
| Prototype, workshop, or app under about ten screens | Layer-first |
| One developer, short lifetime | Layer-first |
| Several developers or teams working in parallel | Feature-first |
| Features are added and removed over time | Feature-first |
| You plan to extract features into packages later | Feature-first |
| Many screens share most of their models | Either, with care |
Layer-first is not wrong for a small app. The cost of changing later is also lower than people fear, because a migration can proceed one feature at a time, as a later chapter shows.
A hybrid you will see in practice
Many real projects use feature-first at the top level and layers inside each feature, which is the tree shown earlier. Inside features/cart/, the data/, domain/, and presentation/ folders are a layer-first split at a smaller scale. The reasoning is that layers are a good way to organize a few dozen files, and a poor way to organize a few thousand.
This also explains where cross-cutting code goes. Code that no single feature owns, such as the HTTP client, the theme, and a generic PriceLabel, belongs in core/. Code that wires features together belongs in app/. Whether something is core or feature-specific is a judgment call, and the next chapters give rules for it.
Summary
Layer-first groups by technical role and works well while the app is small. Feature-first groups by product capability, so one change touches one folder, ownership is explicit, and deletion is clean. The folder layout only pays off when imports between features are controlled, and that is the subject of the dependency rules later in this tutorial.
Exercise
Your team has a layer-first app with 60 screens. A developer adds a "wishlist" capability and the pull request touches files in models/, repositories/, providers/, screens/, and widgets/. A reviewer says the change is hard to review. Explain how a feature-first layout would help, and name one thing that could still make the review hard even after the move.
Show answer
In a feature-first layout, all the wishlist files sit under features/wishlist/, so the reviewer reads one area of the diff and can see the whole capability together. It also becomes clear what the change does not touch. The review can still be hard if wishlist imports another feature's internals, such as catalog/data files, because the diff then spreads across features again and the coupling is hidden inside it. The review is also hard if the author puts wishlist-only code into core/, since that mixes the change into shared code that other features use.
Exercise
A colleague proposes a feature called "buttons" that contains the app's primary and secondary button widgets. Is this a good feature? Where should the code go, and why?
Show answer
No. Buttons are generic UI used by many features, not a product capability a product owner would describe as one feature. They belong in shared code, such as core/widgets/. Putting them in a feature would force other features to import that feature's internals.