Sub topic 7
Nested routes and the back stack
Assume you know Dart null safety, Flutter widgets, and basic Navigator.push and Navigator.pop. This chapter uses go_router to express a parent screen and its detail screen as one URL hierarchy.
A route tree is also a URL tree
A nested route is a GoRoute declared inside another GoRoute. The child path is relative to its parent. In this example, books/:id is nested under /, so the full URL becomes /books/42.
The parent route matches first. The child route then adds the book detail page to the matched route stack.
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
final GoRouter router = GoRouter(
initialLocation: '/',
routes: <RouteBase>[
GoRoute(
path: '/',
name: 'home',
builder: (context, state) => const HomeScreen(),
routes: <RouteBase>[
GoRoute(
path: 'books/:id',
name: 'book',
builder: (context, state) {
final String id = state.pathParameters['id']!;
return BookScreen(id: id);
},
),
],
),
],
);
void main() {
runApp(
MaterialApp.router(
routerConfig: router,
),
);
}
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Books')),
body: Center(
child: ElevatedButton(
onPressed: () => context.push('/books/42'),
child: const Text('Open book 42'),
),
),
);
}
}
class BookScreen extends StatelessWidget {
const BookScreen({super.key, required this.id});
final String id;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Book $id')),
body: const Center(child: Text('Book details')),
);
}
}The :id segment is a path parameter. go_router stores its value in state.pathParameters. The non-leading slash in books/:id matters because the child path is relative. Writing /books/:id would make it an absolute path declaration.
How nesting affects the back stack
When the app starts at /, the stack contains the home page. Calling context.push('/books/42') adds the book page above it. Calling context.pop() removes the book page and reveals home again.
Use push when the user is moving deeper and should be able to return to the current page. Use go when the destination represents the app's new location and the router should rebuild the stack for that location. For example, a bottom navigation destination usually uses go, while opening a detail screen usually uses push.
Nested routes describe URL structure and page relationships. They do not automatically place the child inside a widget in the parent's body. For a shared visual layout, use a shell route or explicitly build that layout.
Common errors
- Put child paths inside the parent's
routeslist. - Do not add a leading slash to a relative child path.
- Use
poponly when a page can be removed. Checkcontext.canPop()when the action might run at the root. - Do not use
extrafor essential URL state. Path parameters can be restored from a deep link, butextracannot.
Exercise
Starting at /, which operation adds /books/42 above the home page, and which operation returns to home?
Show answer
context.push('/books/42') adds the book page, and context.pop() removes it.