Sub topic 3
When One Feature Needs Another
This chapter assumes you know Flutter widgets, Dart import and export, and the repository pattern. Each feature exposes its public API through a barrel file, such as lib/features/cart/cart.dart.
When one feature needs another
Checkout needs the cart's item count. Importing cart presentation code would couple checkout to cart internals. Instead, checkout depends on an interface in core:
// lib/core/contracts/cart_summary.dart
abstract interface class CartSummary {
int get itemCount;
}Checkout accepts a CartSummary in its constructor and never imports the cart feature.
Wiring features together in the app shell
The app shell is the only place that knows every feature. It combines the feature routes and passes a cart-backed CartSummary to checkout.
// 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,
],
);caption: Checkout and cart both depend on the core contract, and only the app shell depends on both features. alt: Arrows show checkout and cart pointing at the CartSummary contract in core, with the app shell pointing at both features.
Mirroring features in the test folder
Place tests in test/features/cart/ so they mirror lib/features/cart/. A reader can find the tests for any file by changing only the root folder name.
Checking boundaries automatically
Rules that exist only in documentation drift over time. Add a CI check that fails when one feature imports another feature's data or presentation folder. The lint tool custom_lint can express this rule, or a short script can search for forbidden import paths.
Moving features into separate packages
When a feature stops changing often, move it to packages/cart/ with its own pubspec.yaml. Dart then treats its lib/src/ folder as private to the package, so other packages cannot import it without the analyzer warning.
Migrating an existing app one feature at a time
Move one feature per pull request, starting with the feature that has the fewest dependencies. While callers still use the old paths, leave a small file at the old location that re-exports the new barrel. Remove that file once every import is updated.
Exercise
Why should checkout depend on CartSummary instead of importing the cart repository directly?
Show answer
A direct import couples checkout to cart internals, so a change in the cart data layer can break checkout. The interface in core lets each feature change independently.