Skip to content

[go_router_builder][go_router] .location, .go(context) and .push(context) are only defined in the generated extension #106790

@ValentinVignal

Description

@ValentinVignal

Use case

Right now, the generation creates an extension on each pages:

// routes.dart
@TypedGoRoute< MyRoute1 >(path: '/my-route-1')
class MyRoute1 extends GoRouteData {
  const MyRoute1();

  @override
  Widget build(BuildContext context) => const MyScreen2();
}

@TypedGoRoute< MyRoute2 >(path: '/my-route-1')
class MyRoute2 extends GoRouteData {
  const MyRoute2();

  @override
  Widget build(BuildContext context) => const MyScreen2();
}
// routes.g.dart
extension $MyRoute1Extension on MyRoute1 {
  static MyRoute1 _fromState(GoRouterState state) => const MyRoute1();

  String get location => GoRouteData.$location(
        '/my-route-1',
      );

  void go(BuildContext context) => context.go(location, extra: this);

  void push(BuildContext context) => context.push(location, extra: this);
}

extension $MyRoute2Extension on MyRoute2 {
  static MyRoute2 _fromState(GoRouterState state) => const MyRoute2();

  String get location => GoRouteData.$location(
        '/my-route-2',
      );

  void go(BuildContext context) => context.go(location, extra: this);

  void push(BuildContext context) => context.push(location, extra: this);
}

But because of that, when manipulating a GoRouteData object, we cannot use go, push or location.

GoRouteData route;

route.go(context); // <- Error

This also prevents us from creating an extension on GoRouteData that reuses those methods:

extension on GoRouteData {
  void customGo(BuildContext context) {
    go(context); // <- Not possible
  } 
}

Proposal

It would be nice if the generated code doesn't work with extension (like what freezed does) for example

abstract class GoRouteMethods {
  void go(BuildContext context);
}

class MyRoute extends GoRouteData with _$MyRoute implements GoRouteMethods {}

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_routerThe go_router packagep: go_router_builderThe go_router_builder packagepackageflutter/packages repository. See also p: labels.team-frameworkOwned by Framework teamtriaged-frameworkTriaged by Framework team

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions