Sub topic 1
Why structure matters as an app grows
A Flutter app with five screens works with almost any folder layout. An app with fifty screens, several teams, and years of releases does not. This chapter explains what goes wrong as a codebase grows, why folder layout is part of the cause, and what a good structure has to provide. The rest of the tutorial builds that structure one piece at a time.
Prerequisites
This chapter assumes you can do the following:
- Build Flutter apps with widgets and navigation, for example with
Navigatororgo_router. - Read Dart imports and know what
exportdoes. - Use one state management approach, such as Provider, Riverpod, or Bloc. The specific one does not matter here.
- Describe the repository pattern: a class that hides where data comes from behind a small interface.
You do not need any knowledge of architecture frameworks or monorepo tooling.
What changes as an app grows
Small apps are cheap to change because a developer can hold the whole app in their head. Three things stop being true as the app grows.
- Nobody knows the whole codebase. A developer touches a screen they have never seen. They need to find every file that belongs to it quickly.
- Many people change the code at once. Two developers working on different features edit the same folders and sometimes the same files. Merge conflicts and accidental breakage rise.
- Old code stays. Features launched two years ago still need bug fixes, and the people who wrote them may have left.
Each of these makes one cost grow faster than the others: the cost of finding code, and the cost of knowing what a change can break. Folder structure is one of the few tools that addresses both directly, because it is the first thing every developer sees.
The layer-first layout and where it hurts
Most tutorials and many first apps group files by technical type. This is called layer-first (or type-first) organization. A project for a shop app might look like this:
lib/
main.dart
models/
product.dart
cart_item.dart
order.dart
user.dart
services/
product_service.dart
cart_service.dart
order_service.dart
providers/
product_provider.dart
cart_provider.dart
order_provider.dart
screens/
product_list_screen.dart
product_detail_screen.dart
cart_screen.dart
checkout_screen.dart
widgets/
product_tile.dart
cart_badge.dart
price_label.dartThis layout is easy to explain and works well at 20 files. At 400 files, each folder holds dozens of unrelated files. The next sections show the specific problems.
Problem 1: one feature is scattered across the tree
To change how the cart works, you open models/cart_item.dart, services/cart_service.dart, providers/cart_provider.dart, screens/cart_screen.dart, and widgets/cart_badge.dart. These sit in five folders, each next to files from other features. Finding all the cart code means searching by file name, and nothing in the layout tells you whether you found everything.
The reverse also hurts. When a feature is removed or rewritten, deleting it means hunting through every folder. Dead code usually stays behind, because nobody is sure it is unused.
Problem 2: any file can import any other file
In a layer-first project every file is a short relative path from every other file. Nothing separates "the cart's private helper" from "something meant to be shared". Over time, imports like these appear:
// lib/screens/checkout_screen.dart
import 'package:flutter/material.dart';
import '../providers/cart_provider.dart';
import '../services/cart_service.dart';
import '../models/cart_item.dart';
import '../widgets/cart_badge.dart';
class CheckoutScreen extends StatelessWidget {
const CheckoutScreen({super.key, required this.cartService});
final CartService cartService;
@override
Widget build(BuildContext context) {
return const Scaffold(body: Center(child: CartBadge()));
}
}The checkout screen now depends on four internals of the cart: its provider, its service, its model, and one of its widgets. If the cart team renames CartService or changes how CartProvider stores items, checkout breaks. The cart team did not know checkout used those classes, because nothing recorded the dependency except the import line.
This is coupling: one piece of code depending on the details of another. A few such links are harmless. Hundreds of them across the app produce a codebase where a small change has unpredictable effects.
Problem 3: circular dependencies
Once any file may import any other, cycles form. The cart provider imports the product service to look up prices. A later change makes the product service import the cart provider to hide out-of-stock items that are already in the cart. Dart tolerates import cycles between files, so the compiler stays quiet. The cost shows up later:
- You cannot test the product service without also constructing the cart provider.
- You cannot move either one into its own package.
- Initialization order bugs appear that only occur in some navigation paths.
Problem 4: shared folders turn into dumping grounds
widgets/ and models/ start as shared code. Because every feature can add to them, they collect widgets that only one screen uses and models that mix fields from three features. A User class grows a field for each feature that needs one: cart discount level, order history count, notification settings. Changing that class now affects everything.
How these problems show up in daily work
You rarely see "bad structure" as an error message. You see its effects:
| Symptom | Likely structural cause |
|---|---|
| A small change needs edits in many folders | One feature is spread across layers |
| Pull requests from different teams conflict often | Teams share the same folders and files |
| Tests need heavy setup to run one class | Hidden dependencies between unrelated classes |
| New developers take weeks to be productive | No visible boundaries to learn feature by feature |
| Nobody dares to delete old code | Unknown imports from elsewhere |
| Full test runs and analyzer runs feel slow | Everything depends on everything, so everything is affected by any change |
None of these is fatal alone. Together they slow every future change, and the slowdown grows with the size of the app.
What a good structure must provide
A structure that scales has to give you four properties. The chapters that follow show how feature-first organization provides each one.
- Locality. The code for one feature is found in one place. A developer can open one folder and see the screens, state, and data access for it.
- Explicit boundaries. It is clear which code is a feature's private business and which code is offered to others. Other code may use only the offered part.
- Controlled dependencies. Dependencies flow in one direction. Features may use shared code, but shared code never depends on a feature, and features do not reach into each other's internals.
- Replaceability. You can rewrite or delete a feature by changing its folder and a small number of wiring points, without a search across the whole project.
Notice that these are properties of the dependency graph, not of folder names. A folder called features/ with unrestricted imports between files gives you locality but no boundaries. Folders help because they give you a place to state and check rules. Later chapters add those rules.
A first look at the target
Here is the same shop app, grouped by feature. Only the cart feature is expanded:
lib/
main.dart
app/
core/
features/
catalog/
cart/
cart.dart
cart_routes.dart
data/
domain/
presentation/
checkout/Everything about the cart sits under features/cart/. The file cart.dart is the only file other features may import. The inner folders keep the usual separation of concerns (screens, business rules, data access) inside the feature instead of across the whole app. You will design this folder in detail in the next chapters. For now, compare what it changes about the import graph:
In the second picture, the cart team can change anything behind cart.dart without breaking checkout. That is the whole point of the structure.
Why not just add discipline?
A common reply is that developers can simply agree not to create these problems. This works for a while in a small team, and fails for predictable reasons:
- Rules that live only in a wiki are not visible at the moment someone writes an import.
- A tight deadline favors the shortest import path, which is usually the wrong one.
- New team members do not know the unwritten rules.
- Violations accumulate quietly, and each one makes the next more acceptable.
A structure turns a rule into something visible in the file tree and, later, something a tool can check. The chapter on enforcement shows how to make a violation fail the build.
When structure is not worth the cost
Feature-first layout adds some ceremony: more folders, barrel files, and routing wiring. For a prototype, a hackathon app, or a single-screen utility, that overhead is not justified. A practical rule of thumb:
- Up to a handful of screens and one developer: a simple layer-first or flat layout is fine.
- Several features, or more than one developer working in parallel: start moving to feature-first.
- Multiple teams, long-lived product: adopt it early and add enforcement.
Moving later costs more than starting early, but moving in small steps is possible. The final chapter covers an incremental migration plan.
Summary
Layer-first folders scatter one feature across many directories, allow every file to import every other file, and lead to hidden coupling, cycles, and shared folders that fill with feature-specific code. These effects raise the cost of finding code and the cost of predicting what a change breaks. A scalable structure provides locality, explicit boundaries, controlled dependency direction, and replaceability. Feature-first organization is the layout this tutorial uses to provide them.
Exercise
In a layer-first project, checkout_screen.dart imports cart_provider.dart and cart_service.dart. Name two problems this creates when the cart team later changes those classes, and say which of the four properties (locality, explicit boundaries, controlled dependencies, replaceability) is missing.
Show answer
First, checkout breaks when the cart team renames or reshapes the provider or service, and the cart team has no visible record that checkout depends on them. Second, the cart code cannot be rewritten, moved, or deleted without searching the whole project for outside users, and the two features cannot be tested independently. The missing property is explicit boundaries: nothing marks the cart's provider and service as private, so another feature can depend on them freely. Controlled dependencies is also missing, because features reach into each other's internals.
Exercise
A teammate says: "We moved all files into features/ folders, so we now have a feature-first structure and no more coupling problems." Why might that claim be wrong, and what would you check?
Show answer
Folders give locality, but they do not stop imports. If files in features/checkout/ still import files such as features/cart/data/cart_service.dart directly, the coupling is unchanged. Check the import lines between features: each feature should import another only through its single public entry file, and shared code should not import any feature. Without a rule, and ideally an automated check, the boundaries exist only on paper.