Skip to content

Type mismatch when enum in RoutePage is provided as query parameter #2148

Description

@gaganyadav80
  1. Declare a route that takes a enum with @QueryParam() tagged.
  2. The generate PageRouteInfo provides the query param from the URI as is which is a String and the args expect enum and this will break the flow.

Actual Result

  • Maybe In this case I am expected to parse the enum by checking the URI in deeplinkTransformer but that makes it hard to manage deeplinks with auto route.

Expected Result

  • I suggest the builder should be able to parse the enum using it's name. And now it is the responsibility of the developer (me) that I use the same name as the value of the query parameter
  • If this is not possible then at least I should be able to define a custom converter in the @QueryParam object. This would be a callback with the queryParameters map as parameter.
  • go_router_builder is able to do this same thing.
Image

Code to reproduce

  1. Example deeplink: https://www.sample.com/cart?type=rm
  2. Route declaration
@RoutePage()
class MfUserCartView extends ConsumerStatefulWidget {
  const MfUserCartView({
    super.key,
    @QueryParam('type') required this.userCartType,
  });

  final MfUserCartTypes? userCartType;

  @override
  ConsumerState<MfUserCartView> createState() => _MfUserCartViewState();
}
  1. Generated RouteInfo
/// generated route for
/// [_i79.MfUserCartView]
class MfUserCartRoute extends _i132.PageRouteInfo<MfUserCartRouteArgs> {
  MfUserCartRoute({
    _i133.Key? key,
    required _i148.MfUserCartTypes? userCartType,
    List<_i132.PageRouteInfo>? children,
  }) : super(
          MfUserCartRoute.name,
          args: MfUserCartRouteArgs(
            key: key,
            userCartType: userCartType,
          ),
          rawQueryParams: {'type': userCartType},
          initialChildren: children,
        );

  static const String name = 'MfUserCartRoute';

  static _i132.PageInfo page = _i132.PageInfo(
    name,
    builder: (data) {
      final queryParams = data.queryParams;
      final args = data.argsAs<MfUserCartRouteArgs>(
          orElse: () =>
              MfUserCartRouteArgs(userCartType: queryParams.get('type'))); // problem is here.
      return _i79.MfUserCartView(
        key: args.key,
        userCartType: args.userCartType,
      );
    },
  );
}

class MfUserCartRouteArgs {
  const MfUserCartRouteArgs({
    this.key,
    required this.userCartType,
  });

  final _i133.Key? key;

  final _i148.MfUserCartTypes? userCartType;

  @override
  String toString() {
    return 'MfUserCartRouteArgs{key: $key, userCartType: $userCartType}';
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions