Skip to content

[go_router_builder] Proposal to improve how we handle extra parameters #117261

Open

Description

@dancamdev

In the current version of go_router_builder this is the only way to handle extra parameters to the route.

class PersonRouteWithExtra extends GoRouteData {
  PersonRouteWithExtra({this.$extra});
  final int? $extra;

  @override
  Widget build(BuildContext context) => PersonScreen(personId: $extra);
}

This comes with some major developer experience drawbacks in my opinion, among which, the biggest are:

  • forced named parameter
  • parameter type must be optional because it must be named and cannot be required
  • parameter must be named $extra

This leads to having roughly the following line every time you need to use the route (which btw wrongly shows a positional argument in the documentation):

PersonRouteWithExtra($extra: Person(name: 'Marvin', age: 42)).go(context);

Ideally, I would have a positional parameter or be able to have a custom named one.

The purpose of this issue is to discuss potential improvements on the API

Activity

  1. dancamdev commented on Dec 17, 2022

    @dancamdev
    Author

    Specifically tackling the

    parameter name must be $extra

    We could define a decorator that's responsible for making sure the generator knows which parameter is the extra one, instead of relying on the name.

    Ideally, something along this line:

    class PersonRouteWithExtra extends GoRouteData {
      PersonRouteWithExtra({this.$extra});
      @Extra() final int? id;
    
      @override
      Widget build(BuildContext context) => PersonScreen(personId: id);
    }

    Which would result in a much more clean way of using the route.

    PersonRouteWithExtra(id: person.id).go(context);
  2. darshankawar commented on Dec 19, 2022

    @darshankawar
    Member

    @dancamdev
    Can you take a look at this issue and see if your use case is same ?

  3. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Dec 19, 2022
  4. dancamdev commented on Dec 19, 2022

    @dancamdev
    Author

    @dancamdev

    Can you take a look at this issue and see if your use case is same ?

    My proposed solution is exactly the same, yes.

    Still, this issue aims to discuss broader fixes to extra - not only the variable name. Passing it as a positional parameter or requiring it if named and non-nullable would be what it really makes it more convenient than go_router.

    At the moment I see too many compromises. I tried go_router_builder, I've seen it got in the way of what I was trying to achieve and went back to go_router

  5. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Dec 19, 2022
  6. darshankawar commented on Dec 19, 2022

    @darshankawar
    Member

    Thanks for the update. I will keep this issue open and treat it as feature request based on above comment.

  7. added
    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 Dec 19, 2022
  8. added
    P3Issues that are less important to the Flutter project
    on Jan 10, 2023
  9. 5 remaining items

  10. lucavenir commented on Mar 13, 2026

    @lucavenir

    the changelog links this issue as it's resolved - is that actually the case?

    I tried the example, it doesn't really work for me. I mean, the basic example works, but if I annotate a class using @freezed and generate the .fromJson and .toJson on it, and try to use such class as a typed route, the builder complains:

    lib/router.dart go_router_builder
    E The parameter type `MyFreezedClass` not have a supported fromJson definition.
      package:something/src/my_freezed_class.dart:18:16
         ╷
      18 │ abstract class MyFreezedClass with _$MyFreezedClass {
         │                ^^^^^^^^^^^^^^^^^^^^^^^
         ╵
    

    I'd be great to understand what's causing this, exactly.

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 Flutterp: 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