Skip to content

[go_router_builder] Define routes in separate files #122258

Open

Description

@mikuhl-dev

Use case

It looks like with go_router_builder, you are supposed to define all your routes into this one gigantic file, this also leads to making two different classes that essentially represent the same thing, like a HomeRoute, and HomePage that you pass in the HomeRoutes build method. At this point the code generation only solves the compile time checking, and instead leaves you with even MORE boilerplate.

Proposal

We should be able to define route data in separate files.

Activity

  1. added
    in triagePresently being triaged by the triage team
    c: new featureNothing broken; request for a new capability
    packageflutter/packages repository. See also p: labels.
    c: proposalA detailed proposal for a change to Flutter
    and removed
    in triagePresently being triaged by the triage team
    on Mar 9, 2023
  2. added
    P3Issues that are less important to the Flutter project
    on Mar 14, 2023
  3. dancamdev commented on Apr 8, 2023

    @dancamdev

    I've given it a go, technically made it work but with a catch.

    The only technical issue preventing us from having multiple definition files is that go_router_builder generates a list always called $appRoutes.

    So, I went ahead and instead of it always being $appRoutes, I've set the variable name to be based on the file name the generation is starting from.

    Therefore, given I have the following structure:

    • auth.dart part of auth.g.dart containing the /home route.
    • unauth.dart part of unauth.g.dart containing /login and /signup routes.

    I can easily define the router as follows:

    final GoRouter _router = GoRouter(
        routes: <GoRoute>[...$authRoutes, ...$unauthRoutes],
      );
    

    The drawback I see here, is that it being file based you could potentially end up with long and weird names. Such as:

    • auth.dart -> $authRoutes
    • all_types.dart -> $all_typesRoutes
    • auth_routes.dart -> $auth_routesRoutes
    • PascalCaseRoutes.dart -> $pascalCaseRoutesRoutes

    Happy to discuss this further and open a PR with these changes if you want to take a deeper look into it.

  4. lmapii commented on Jun 1, 2023

    @lmapii

    I'm actually trying for the first time to use go_route_builder and my first attempt was to attach the routes to the screens since this made most sense to me. I was surprised that I had to place all my route classes in the same file as the router.

  5. JasCodes commented on Aug 24, 2023

    @JasCodes

    This is one of the obious things that make me pull out hair. Forcing to put all routes in single file. Don't know who thought it was great idea.

    Look at https://pub.dev/packages/katana_router how single AOP based can be...

  6. vykes-mac commented on Nov 9, 2023

    @vykes-mac

    I've given it a go, technically made it work but with a catch.

    The only technical issue preventing us from having multiple definition files is that go_router_builder generates a list always called $appRoutes.

    So, I went ahead and instead of it always being $appRoutes, I've set the variable name to be based on the file name the generation is starting from.

    Therefore, given I have the following structure:

    • auth.dart part of auth.g.dart containing the /home route.
    • unauth.dart part of unauth.g.dart containing /login and /signup routes.

    I can easily define the router as follows:

    final GoRouter _router = GoRouter(
        routes: <GoRoute>[...$authRoutes, ...$unauthRoutes],
      );
    

    The drawback I see here, is that it being file based you could potentially end up with long and weird names. Such as:

    • auth.dart -> $authRoutes
    • all_types.dart -> $all_typesRoutes
    • auth_routes.dart -> $auth_routesRoutes
    • PascalCaseRoutes.dart -> $pascalCaseRoutesRoutes

    Happy to discuss this further and open a PR with these changes if you want to take a deeper look into it.

    @dancamdev Where did you set it so it generate the variable based on filename? Did you manually change the variable names? does it gets overwritten whenever you re-run the build_runner?

  7. 11 remaining items

  8. mitryp commented on Jul 7, 2025

    @mitryp
    Contributor

    Hello,

    While we wait for the official implementation, I’ve developed go_router_aggregator to work around the current limitation. It gathers the generated $appRoutes from across the project and combines them into one $aggregatedRoutes collection.

    You can already try it:
    https://pub.dev/packages/go_router_aggregator

    Feedback and contributions are very welcome on the package repository.

  9. ManuelRauber commented on Oct 15, 2025

    @ManuelRauber

    Will there ever be an implementation for this? @chunhtai

    It absolutely prevents using go_router with pub workspaces and a clear separation.

  10. chunhtai commented on Oct 15, 2025

    @chunhtai
    Contributor

    I don't have any plan for this since I am currently helping with decoupling design library. If you want to work on this, feel free to submit a pr and pin me for review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Issues that are less important to the Flutter projectc: new featureNothing broken; request for a new capabilityc: proposalA detailed proposal for a change to Fluttercustomer: crowdAffects or could affect many people, though not necessarily a specific customer.p: go_router_builderThe go_router_builder packagepackageflutter/packages repository. See also p: labels.team-ecosystemOwned by Ecosystem teamtriaged-ecosystemTriaged by Ecosystem team

    Type

    No type

    Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions