Skip to content

[go_router_builder] Support default value for the route parameters #114406

@ValentinVignal

Description

@ValentinVignal

Use case

It would be nice to support default values like secondParameter in the snippet below:

@TypedGoRoute<HomeRoute>(path: '/')
class HomeRoute extends GoRouteData {
  const HomeRoute({
    this.firstParameter,
    this.secondParameter = 'default',
  });

  final String? firstParameter;
  final String secondParameter;

  @override
  Widget build(BuildContext context) {
    return const SizedBox();
  }
}

Right now, it is not supported and throws compilation errors:

  error • The argument type 'String?' can't be assigned to the parameter type 'String' • lib/routes.g.dart:21:26 • argument_type_not_assignable

in

extension $HomeRouteExtension on HomeRoute {
  static HomeRoute _fromState(GoRouterState state) => HomeRoute(
        firstParameter: state.queryParams['first-parameter'],
        secondParameter: state.queryParams['second-parameter'], // <- The argument type 'String?' can't be assigned to the parameter type 'String'.
      );
}

You can also checkout https://github.com/ValentinVignal/flutter_app_stable/tree/go-router-builder/default-value-for-parameter

Proposal

Default values for route parameters could be supported.

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.r: fixedIssue is closed as already fixed in a newer version

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions