Sub topic 3
Defining routes and paths
This chapter assumes you know basic Dart null safety, can build Flutter widgets, and have used Navigator.push and Navigator.pop. It also assumes you can expose app state with ChangeNotifier or a similar class.
A route is a declared destination in your app. In go_router, routes form a tree. Each route has a path, and the path describes the location that displays the route. Locations look like web URLs, such as /, /settings, or /books/42.
Declaring flat and nested routes
Add go_router to pubspec.yaml, then create a GoRouter. Attach the router to MaterialApp.router.
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);
},
),
],
),
GoRoute(
path: '/settings',
name: 'settings',
builder: (context, state) => const SettingsScreen(),
),
],
);
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp.router(routerConfig: router);
}
}
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
@override
Widget build(BuildContext context) {
return const Scaffold(
body: Center(child: Text('Home')),
);
}
}
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: Center(child: Text('Showing book $id')),
);
}
}
class SettingsScreen extends StatelessWidget {
const SettingsScreen({super.key});
@override
Widget build(BuildContext context) {
return const Scaffold(
body: Center(child: Text('Settings')),
);
}
}The root route uses /, while the nested book route uses books/:id without a leading slash. go_router joins the child path to its parent, producing locations such as /books/42.
| Path pattern | Example location | Meaning |
|---|---|---|
| `/` | `/` | The home page |
| `/settings` | `/settings` | A fixed destination |
| `books/:id` | `/books/42` | A child route with a variable segment |
Path segments and parameters
A segment beginning with a colon is a path parameter. In books/:id, the id value is captured from the location. The route reads it through state.pathParameters.
Path parameters identify a required resource. A book screen cannot identify a specific book without an ID. Use query parameters for optional values, such as a filter or sort order. Query parameters are read from state.uri.queryParameters, and they do not need to be declared in the route path.
How route matching works
When the app receives a location, go_router compares it with the declared route tree. It matches fixed segments exactly and assigns values from parameter segments.
A location that matches no route produces an unknown-location error. Later chapters will add a custom errorBuilder for this case.
Exercise
What location matches the nested route books/:id when the book ID is 17?
Show answer
The location is /books/17, and state.pathParameters['id'] contains the string '17'.