Sub topic 9
Each Feature Owns Its Routes
This chapter assumes you know Flutter widgets and navigation, Dart imports and exports, and the basic repository pattern. The examples use go_router, but the ownership rule also applies to other routing packages.
Why route ownership matters
A large app often starts with one router file:
lib/
app/
router.dart
features/
catalog/
cart/
profile/At first, this is simple. Later, router.dart becomes a list of every page, redirect, route parameter, and nested navigator in the app. Adding a feature means editing a central file and importing that feature's private presentation code.
Feature-first structure gives each feature responsibility for its own routes. The app shell only combines the route lists. This keeps route details close to the pages they expose.
Define routes inside the feature
Create a route file beside the feature's public entry file. The feature imports its own pages and declares the paths it owns.
// lib/features/cart/cart_routes.dart
import 'package:go_router/go_router.dart';
import 'presentation/cart_page.dart';
final cartRoutes = <RouteBase>[
GoRoute(
path: '/cart',
name: 'cart',
builder: (context, state) => const CartPage(),
),
];The route list is a value that the app can add to its root list. The feature does not create the global GoRouter instance. That remains an application concern because it may include authentication redirects, error pages, and application-wide navigation settings.
A feature can also own nested routes:
final catalogRoutes = <RouteBase>[
GoRoute(
path: '/catalog',
name: 'catalog',
builder: (context, state) => const CatalogPage(),
routes: [
GoRoute(
path: 'products/:id',
name: 'product-details',
builder: (context, state) {
final id = state.pathParameters['id']!;
return ProductDetailsPage(productId: id);
},
),
],
),
];The nested path is products/:id, not /products/:id, because it is relative to /catalog.
Combine routes in the app shell
The application router imports the public feature libraries and combines their route lists.
// 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,
],
);The spread operator (...) inserts every route from a list into the surrounding list. The app shell owns composition, while each feature owns route definitions.
Keep the public boundary small
The feature's barrel file exports its routes but not its private page implementation.
// lib/features/cart/cart.dart
export 'cart_routes.dart';Other code imports cart.dart, rather than importing presentation/cart_page.dart directly. This makes the intended dependency visible and gives you one place to change the feature's public API later.
Common errors
- Defining a feature's routes in both the feature and the app router creates duplicate paths.
- Using an absolute path for a nested route breaks the intended URL hierarchy.
- Importing private pages from
app/router.dartbypasses the feature boundary. - Registering two routes with the same name causes confusing navigation failures.
- Putting authentication redirects in every feature duplicates policy. Keep app-wide redirects in the app shell.
Exercise
Where should the /cart route be declared, and where should it be added to the global router?
Show answer
The cart feature should declare it in cart_routes.dart. The app shell should add the exported cartRoutes list to GoRouter.routes.