Sub topic 2
Structuring the Route Tree with Sub-routes
This chapter assumes you know Dart and Flutter widgets, Scaffold, basic Navigator concepts, and the basic go_router types GoRouter, GoRoute, and context.go. The goal is to model related pages as a route tree before adding tab navigation.
A route tree represents page relationships
A GoRoute can contain a routes list. Each route in that list is a sub-route of its parent. This relationship gives you a predictable URL and a navigation stack.
For example:
/projectsshows the project list./projects/42shows project 42./projects/42/tasks/7shows task 7 inside project 42.
The child path is relative to its parent. Write :projectId, not / :projectId or another path beginning with /.
A path segment beginning with : is a path parameter. It matches one value and makes that value available through state.pathParameters.
Define parent and child routes
The following complete example defines a small Flutter app with nested project and task routes.
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
final router = GoRouter(
initialLocation: '/projects',
routes: [
GoRoute(
path: '/projects',
builder: (context, state) => const ProjectsPage(),
routes: [
GoRoute(
path: ':projectId',
builder: (context, state) {
final projectId = state.pathParameters['projectId']!;
return ProjectPage(projectId: projectId);
},
routes: [
GoRoute(
path: 'tasks/:taskId',
builder: (context, state) {
final taskId = state.pathParameters['taskId']!;
return TaskPage(taskId: taskId);
},
),
],
),
],
),
],
);
void main() => runApp(const App());
class App extends StatelessWidget {
const App({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp.router(routerConfig: router);
}
}
class ProjectsPage extends StatelessWidget {
const ProjectsPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Projects')),
body: Center(
child: ElevatedButton(
onPressed: () => context.go('/projects/42'),
child: const Text('Open project 42'),
),
),
);
}
}
class ProjectPage extends StatelessWidget {
const ProjectPage({super.key, required this.projectId});
final String projectId;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Project $projectId')),
body: Center(
child: ElevatedButton(
onPressed: () => context.go('/projects/$projectId/tasks/7'),
child: const Text('Open task 7'),
),
),
);
}
}
class TaskPage extends StatelessWidget {
const TaskPage({super.key, required this.taskId});
final String taskId;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Task $taskId')),
body: Center(child: Text('Viewing task $taskId')),
);
}
}When the app navigates to /projects/42/tasks/7, go_router matches the route from the root downward. It extracts 42 as projectId and 7 as taskId.
Choose `go` or `push`
context.go(location) changes the app's location to the supplied URL. It is useful when the URL itself represents the new application state.
context.push(location) adds a page to the current navigation stack. The previous page remains available when the user presses Back.
For nested detail pages, either can be correct. Use push when the user is drilling into a page and should return through the detail history. Use go when a menu, link, or redirect should select a specific location directly.
Check the route hierarchy
Before adding a StatefulShellRoute, decide which pages belong together. A tab can later own a subtree such as /projects and /projects/:projectId, while another branch can own /settings and /settings/account. Keeping related routes under one parent makes each branch's navigation stack easier to understand.
Exercise
What full URL matches the nested path tasks/:taskId under /projects/:projectId when projectId is 42 and taskId is 7?
Show answer
/projects/42/tasks/7