Sub topic 3
What Goes Inside a Feature Folder
This chapter assumes you can build Flutter widgets, use Dart imports and exports, navigate between screens, and understand the basic repository pattern. It also assumes familiarity with at least one state management approach.
A feature folder contains the code needed for one user-facing capability, such as a catalog, cart, profile, or checkout. The feature owns its screens, state, business rules, and data access. This keeps related code close together instead of spreading it across app-wide screens, models, and services folders.
A feature's basic anatomy
A practical feature usually has three parts:
presentation/contains pages, widgets, and feature-specific state.domain/contains business concepts and interfaces. It should not depend on Flutter or a database.data/contains repository implementations, API clients, database code, and data models.
The feature can also have a public entry file and a route file:
lib/features/cart/
cart.dart
cart_routes.dart
data/
in_memory_cart_repository.dart
domain/
cart_item.dart
cart_repository.dart
presentation/
cart_controller.dart
cart_page.dart
cart_item_tile.dartThe folder structure is a guide, not a requirement to create empty layers. If a small feature has no remote data, it may not need a data/ folder yet.
How the parts depend on each other
The presentation layer uses domain interfaces. The data layer implements those interfaces. This direction keeps the user interface independent from a specific API or storage system.
For example, the domain defines what a cart repository can do:
// lib/features/cart/domain/cart_repository.dart
import 'cart_item.dart';
abstract interface class CartRepository {
Future<List<CartItem>> loadItems();
Future<void> addItem(CartItem item);
}
// lib/features/cart/data/in_memory_cart_repository.dart
import '../domain/cart_item.dart';
import '../domain/cart_repository.dart';
class InMemoryCartRepository implements CartRepository {
final List<CartItem> _items = [];
@override
Future<List<CartItem>> loadItems() async => List.unmodifiable(_items);
@override
Future<void> addItem(CartItem item) async {
_items.add(item);
}
}A controller or notifier in presentation/ receives a CartRepository. It does not need to know whether the implementation uses memory, HTTP, or SQLite.
What belongs in shared code?
Put code in core/ or another shared area only when it has no meaningful ownership by one feature. Examples include an HTTP client, app theme, common error type, or reusable button.
A cart-specific CartItemTile belongs in cart/presentation/, even if it looks reusable today. Move it to shared code after a second feature needs the same behavior and the abstraction is clear.
A feature's boundary
A feature should expose a small public surface. Other code should import cart.dart, not files deep inside the cart folder.
// lib/features/cart/cart.dart
export 'cart_routes.dart';
export 'domain/cart_item.dart';
export 'domain/cart_repository.dart';This file is a barrel file: a library that re-exports selected files. It documents the feature's supported API and lets you change private files without changing every import.
A quick design check
Before adding a file, ask:
| Question | If yes |
|---|---|
| Does it serve one user capability? | Put it inside that feature. |
| Does it work across unrelated features? | Consider shared code. |
| Does it contain business rules? | Put it in `domain/`. |
| Does it call an API or database? | Put it in `data/`. |
Exercise
Where should a cart-specific widget that displays one item usually live?
Show answer
In lib/features/cart/presentation/, because it belongs to the cart feature's user interface.