Skip to content

[go_router_builder] Allow the user to specify a type converter of the parameters #112152

Closed
flutter/packages
#11068

Description

@ValentinVignal

This is kind of an extension to #108437

Use case

I'm using go router builder and I have a route with optional parameters:

enum MyEnum {
  myFirstEntry,
  mySecondEntry,
}

@TypedGoRoute<MyRoute>(path: '/my-path')
class MyRoute extends GoRouteData {
  const MyRoute({
    this.myBoolean = false,
    this.myEnum,
  });

  final bool myBoolean;

  final MyEnum? myEnum;

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

If I want myBoolean to be true, it forces the url to be my-boolean=true. But I might want another formatting, like myBoolean=true (parameter not in kebab-case) or my-boolean=t (t instead of true).

There is the same issue with myEnum, it forces the url parameter to be my-enum (but I could want to keep myEnum) and the values to be kebab-case like my-first-route (but I could want to keep myFirstEntry : myEnum=myFirstEntry).

In my case, those parameters are set by a thrid party of my app and I cannot control them or change them, which prevents me from using go_router_builder.

Proposal

It could be nice to allow us to specify the formatting/create a converter like json_serializable and JsonConverter.

enum MyEnum {
  myFirstEntry,
  mySecondEntry,
}

@TypedGoRoute<MyRoute>(path: '/my-path')
class MyRoute extends GoRouteData {
  const MyRoute({
    this.myBoolean = false,
    this.myEnum,
  });
  
  @TypedGoRouteParameter<bool>(name: 'my-boolean')
  final bool myBoolean;
  
  @MyEnumConverter()
  final MyEnum? myEnum;

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


class MyEnumConverter extends TypedGoRouteParameter<MyEnum> {
  class MyEnumConverter(super.name);

  @override
  String? toUrlString(MyEnum myEnum) {
    // Return the correct format here.
    // If `null` is returned, fall back to the existing behavior.
  }

  @override
  MyEnum? fromUrlString(String value) {
    // Return the correct value here.
    // If `null` is returned, fall back to the existing behavior.
  }
}

Activity

  1. added
    in triagePresently being triaged by the triage team
    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 Sep 22, 2022
  2. added
    P3Issues that are less important to the Flutter project
    on Sep 27, 2022
  3. chunhtai commented on Sep 29, 2022

    @chunhtai
    Contributor

    looks like this can be solved by #110781

  4. moved this to Done in GoRouteron Sep 29, 2022
  5. github-actions commented on Oct 13, 2022

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  6. locked as resolved and limited conversation to collaborators on Oct 13, 2022
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.

    Type

    No type

    Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions