Sub topic 11
Wiring Features Together in the App Shell
Assume you know Flutter widgets and navigation, Dart imports and export, one state-management approach, and the repository pattern. This chapter focuses on composition: the app shell connects features without taking ownership of their internal screens, repositories, or state.
What the app shell should own
The app shell is the small amount of code that assembles the application. It typically owns:
- App-wide configuration, such as themes and localization.
- The router and top-level navigation.
- Shared services and repository implementations.
- Dependency injection, which means supplying an object where another object needs it.
- Application-wide state, such as the current user session.
A feature should own its pages, feature state, routes, and data access. The shell connects these pieces but should not duplicate them.
| Concern | Feature owns | App shell owns |
|---|---|---|
| Cart page | Page and view state | No cart layout |
| Cart routes | Route declarations | Route composition |
| Cart repository | Interface and implementation | Which implementation to use |
| Theme | Feature-specific styling | Global theme |
| Session | Session-dependent behavior | Current session object |
Compose routes at the boundary
Each feature exports its routes through its public entry file. The router imports those public files and combines the route lists.
// lib/app/router.dart
import 'package:go_router/go_router.dart';
import '../features/cart/cart.dart';
import '../features/catalog/catalog.dart';
GoRouter createRouter() {
return GoRouter(
routes: [
...catalogRoutes,
...cartRoutes,
],
);
}The spread operator (...) inserts every route from a list into the surrounding list. The router knows that the catalog and cart have routes, but it does not need to know how their pages are implemented.
The app entry point can then create the router and start the application:
// lib/app/app.dart
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
class App extends StatelessWidget {
const App({super.key, required this.router});
final GoRouter router;
@override
Widget build(BuildContext context) {
return MaterialApp.router(
routerConfig: router,
theme: ThemeData(colorSchemeSeed: Colors.indigo),
);
}
}// lib/main.dart
import 'package:flutter/widgets.dart';
import 'app/app.dart';
import 'app/router.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
final router = createRouter();
runApp(App(router: router));
}This arrangement keeps startup wiring in one place. A feature can change its page or route builder without requiring changes to main.dart.
Supply dependencies once
The shell should choose concrete implementations at the composition root. A composition root is the place where abstract interfaces receive their concrete objects.
For example, the shell might create a remote cart repository, then pass it to a cart controller. The controller and page should depend on CartRepository, not on HTTP code. This keeps tests simple and prevents widgets from constructing services during build.
Share state through contracts
When two features need the same information, prefer a small shared contract over a direct import of one feature's internals. For example, checkout can depend on CartSummary from core/contracts, while the shell supplies a cart-backed implementation.
The shell is also a suitable place to provide session state or app-wide services. Keep feature-specific state below that boundary. A catalog filter should remain inside catalog, even if the catalog is displayed below the app shell.
Check the wiring
When adding a feature, ask:
- Does the feature expose one public entry file?
- Does the app shell import only that public file?
- Are concrete dependencies created at the composition root?
- Does shared state have a small, stable interface?
- Can the feature be tested without starting the whole application?
Exercise
Where should the concrete implementation of a feature repository usually be selected?
Show answer
In the app's composition root, such as main.dart or an app-level dependency container. The feature should depend on the repository interface.