Type-safe routes topic

Instead of using URL strings to navigate, go_router supports type-safe routes using the go_router_builder package.

To get started, add go_router_builder, build_runner, and build_verify to the dev_dependencies section of your pubspec.yaml:

  go_router_builder: any
  build_runner: any
  build_verify: any

Then extend the GoRouteData class for each route in your app and add the TypedGoRoute annotation:

import 'package:go_router/go_router.dart';

part 'go_router_builder.g.dart';

    path: '/',
    routes: [
        path: 'song/:id',
class HomeScreenRoute extends GoRouteData {
  Widget build(BuildContext context, GoRouterState state) {
    return const HomeScreen();

class SongRoute extends GoRouteData {
  final int id;

  const SongRoute({
    required this.id,

  Widget build(BuildContext context, GoRouterState state) {
    return SongScreen(songId: id.toString());

To build the generated files (ending in .g.dart), use the build_runner command:

flutter pub global activate build_runner
flutter pub run build_runner build

To navigate, construct a GoRouteData object with the required parameters and call go():

  onPressed: () {
    const SongRoute(id: 2).go(context);
  child: const Text('Go to song 2'),

For more information, visit the go_router_builder package documentation.


GoRouteData Type-safe routes
A class to represent a GoRoute in Type-safe routing.