A builder to leverage GoRouter’s type-safe routes by splitting route declarations out from your router configuration and
then aggregating all generated $appRoutes
getters into a single $aggregatedRoutes
collection. It also serves as a
workaround for flutter/flutter#122258.
- Automatic aggregation: Detects every
$appRoutes
in your generated files and consolidates them into a single$aggregatedRoutes
collection. - Configurable output path: Control where the final routes file is generated via builder options.
If you don't already use go_router
and go_router_builder
, install them:
flutter pub add go_router dev:build_runner dev:go_router_builder dev:build_verify
Next, add this plugin to your project:
flutter pub add dev:go_router_aggregator
This plugin uses build_runner
to generate code.
By default, output will be written to lib/routes.g.dart
.
You can use global_options
in your project’s build.yaml
(next to pubspec.yaml
) to change its location:
global_options:
go_router_aggregator|aggregate_app_routes:
options:
# Path relative to lib/ where the file will be generated
output: "navigation/app_routes.g.dart"
-
Define your routes using GoRouter code generation (e.g.
@TypedGoRoute
,@TypedShellRoute
) in any files it's convenient for you (for example, together with your widgets). -
Run the codegen:
dart run build_runner build -d
Alternatively, start the file system watcher to rebuild automatically on every change:
dart run build_runner watch -d
-
Import the path you set in build.yaml (or
lib/routes.g.dart
by default) and use the$aggregatedRoutes
in your router:import 'navigation/app_routes.g.dart'; final router = GoRouter( initialLocation: const HomeRoute().location, routes: $aggregatedRoutes, // generated routes list );
A complete usage example can be found on GitHub.
// lib/navigation/app_routes.g.dart
// GENERATED by go_router_aggregator - do NOT edit by hand - contents will be overridden
// ignore_for_file: type=lint
import 'package:go_router/go_router.dart';
import 'package:my_app/features/home/home_page.dart' as _home_page0;
import 'package:my_app/features/settings/settings_page.dart' as _settings_page0;
List<RouteBase> get $aggregatedRoutes => [
..._home_page0.$appRoutes,
..._settings_page0.$appRoutes,
];
Contributions are very welcome! If you find a bug or have a feature request, please open an issue. If you’d like to contribute code, fork the repo and open a pull request.
Before submitting a PR:
- Make sure all existing tests pass
dart run test
- Format your changes with
dart format .
- Add tests for any new behavior.
Thank you for helping make go_router_aggregator
even better!