Skip to content

Aggregates GoRouter's generated type-safe routes into one importable file, separating your route declarations from the router setup.

License

Notifications You must be signed in to change notification settings

mitryp/go_router_aggregator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Dart Tests pub package package publisher

Define your type-safe routes where it makes sense

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.


Features

  • 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.

Installation

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.


Configuration

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"

Usage

  1. 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).

  2. 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
  3. 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.


Example Builder Output

// 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,
];

Contributing

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!

About

Aggregates GoRouter's generated type-safe routes into one importable file, separating your route declarations from the router setup.

Topics

Resources

License

Stars

Watchers

Forks