Sub topic 16
Type-safe routes with go_router_builder
This chapter assumes Dart null safety, Flutter widgets, Navigator basics, and familiarity with ChangeNotifier. It also assumes that you have already used go_router with ordinary GoRoute objects.
A large route table can become difficult to maintain when paths are plain strings:
context.go('/books/$id');A typo in the path or a missing parameter is usually found only at runtime. go_router_builder lets you describe routes with Dart classes. The builder generates the route table and navigation methods from those classes. Constructor parameters then represent route parameters, so the Dart analyzer can catch many mistakes before the app runs.
Configure code generation
Add these packages to pubspec.yaml:
dependencies:
flutter:
sdk: flutter
go_router: ^14.0.0
dev_dependencies:
build_runner: ^2.4.0
go_router_builder: ^2.7.0The exact versions should match the versions supported by your Flutter SDK. Create a Dart file for the route declarations. The part statement names the generated file.
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
part 'routes.g.dart';
@TypedGoRoute<HomeRoute>(
path: '/',
routes: <TypedGoRoute<GoRouteData>>[
TypedGoRoute<BookRoute>(path: 'books/:id'),
],
)
class HomeRoute extends GoRouteData {
const HomeRoute();
@override
Widget build(BuildContext context, GoRouterState state) {
return const HomeScreen();
}
}
class BookRoute extends GoRouteData {
const BookRoute({required this.id});
final String id;
@override
Widget build(BuildContext context, GoRouterState state) {
return BookScreen(id: id);
}
}@TypedGoRoute is an annotation that declares a route path and connects that path to a Dart class. :id is a path parameter. The matching class has an id field, so the generated code reads the value from the URL and passes it to the constructor.
Run the generator from the project directory:
dart run build_runner build --delete-conflicting-outputsThe generated file contains $appRoutes. Use that list when creating the router:
final GoRouter router = GoRouter(
routes: $appRoutes,
);
void main() {
runApp(
MaterialApp.router(
routerConfig: router,
),
);
}Navigate with generated methods
After generation, navigate by constructing a route object:
BookRoute(id: '42').go(context);This creates /books/42 from the declaration. To add the book page on top of the current stack, use:
BookRoute(id: '42').push(context);The distinction remains the same as with untyped navigation:
gochanges the location and rebuilds the matching route stack.pushadds a page above the current page.popremoves the current page.
Query parameters can also be represented by typed route fields with the @QueryParam annotation:
class BookRoute extends GoRouteData {
const BookRoute({
required this.id,
this.tab = 'details',
});
final String id;
@QueryParam('tab')
final String tab;
@override
Widget build(BuildContext context, GoRouterState state) {
return BookScreen(id: id);
}
}BookRoute(id: '42', tab: 'reviews').go(context) produces a URL containing ?tab=reviews.
How the pieces connect
Common errors
- Forgetting the
partstatement prevents the generated declarations from being found. - Failing to run
build_runnerleaves generated code out of date. - Using a path parameter name that does not match the constructor field causes incorrect generation or a compile-time error.
- Treating
extraas a replacement for typed URL parameters makes deep links less reliable. Use typed path and query parameters for data that must survive a restart or be shared as a URL.
Exercise
What navigation call should you use when a typed route should replace the current location instead of adding another page to the stack?
Show answer
Use the generated route object's go(context) method, such as BookRoute(id: '42').go(context).