Sub topic 2
Feature-First Project Structure for Large Flutter Apps
This chapter assumes you know Flutter widgets, Dart imports, and the export directive. You have used one state management approach and understand the repository pattern.
Repositories and data sources in a feature
A repository is an abstract interface that the rest of a feature uses to load and save data. A data source is the concrete code that talks to a backend, a local database, or memory. Keep the interface in domain/ and the implementation in data/, so presentation code depends only on the abstraction.
// 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);
}
}Replacing InMemoryCartRepository with an HTTP-backed class changes only the data/ folder. Presentation code stays the same.
Keeping state inside the feature
Create state objects, such as a notifier or bloc, inside the feature and pass them down the widget tree. Avoid global singletons that other features read directly. The feature owns its state, and other features receive only what the public API exposes.
Dependency rules between features and shared code
Apply these rules:
- Features may import
core/. Features must not import each other's internals. core/must never import a feature.- Presentation and data depend on domain. Domain depends on neither.
When checkout needs a cart count, define a small interface in core/contracts/. The app shell then supplies a cart-backed implementation.
// lib/core/contracts/cart_summary.dart
abstract interface class CartSummary {
int get itemCount;
}Exposing a feature through one public file
A barrel file lists what a feature exposes. Other code imports only the barrel.
// lib/features/cart/cart.dart
export 'cart_routes.dart';
export 'domain/cart_item.dart';
export 'domain/cart_repository.dart';Each feature owns its routes
Each feature declares its routes, and the app shell combines them.
// lib/features/cart/cart_routes.dart
import 'package:go_router/go_router.dart';
import 'presentation/cart_page.dart';
final cartRoutes = <RouteBase>[
GoRoute(
path: '/cart',
builder: (context, state) => const CartPage(),
),
];
// lib/app/router.dart
import 'package:go_router/go_router.dart';
import '../features/cart/cart.dart';
import '../features/catalog/catalog.dart';
final router = GoRouter(
routes: [
...catalogRoutes,
...cartRoutes,
],
);Exercise
Where should the CartRepository interface live, and why?
Show answer
In features/cart/domain/. Presentation and data code both depend on the abstraction, so the data implementation can change without touching UI code.