Skip to content

[go_router_builder] Allow required query parametersΒ #126796

@ValentinVignal

Description

@ValentinVignal

Is there an existing issue for this?

Use case

In some scenario, some routes (ex: /my-route/:route-id) requires to always have a parameter (ex: /my-route/:route-id?my-param=my-value)

I would like to enforce the generated code and developers to always specify this parameter:

MyRoute(routeId: 'routeId', myParam: 'myValue').location; // <- OK
MyRoute(routeId: 'routeId').location; // <- KO: `myParam` is required

by doing:

@TypedRoute<MyRoute>(path: '/my-route/:route-id')
class MyRoute extends GoRouteData {
  MyRoute({
    required this.routeId,
    required this.myParam,
  });

  final String routeId;
  final String myParam;
}

Right now, I can only use asserts:

@TypedRoute<MyRoute>(path: '/my-route/:route-id')
class MyRoute extends GoRouteData {
  MyRoute({
    required this.routeId,
    this.myParam = '',
  }): assert(myParam.isNotEmpty);

  final String routeId;
  final String myParam;
}

but this doesn't give a check at compilation time, it only fails at running time.

Proposal

It would be nice to support required query parameters:

@TypedRoute<MyRoute>(path: '/my-route/:route-id')
class MyRoute extends GoRouteData {
  MyRoute({
    required this.routeId,
    required this.myParam,
  });

  final String routeId;
  final String myParam;
}

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.

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions