Skip to content

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

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:

enumMyEnum {
myFirstEntry,
mySecondEntry,
}
@TypedGoRoute<MyRoute>(path:'/my-path')
classMyRouteextendsGoRouteData {
constMyRoute({
this.myBoolean =false,
this.myEnum,
});
finalbool myBoolean;
finalMyEnum? myEnum;
@overrideWidgetbuild(BuildContext context) =>constMyScreen();
}

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.

enumMyEnum {
myFirstEntry,
mySecondEntry,
}
@TypedGoRoute<MyRoute>(path:'/my-path')
classMyRouteextendsGoRouteData {
constMyRoute({
this.myBoolean =false,
this.myEnum,
});
@TypedGoRouteParameter<bool>(name:'my-boolean')
finalbool myBoolean;
@MyEnumConverter()
finalMyEnum? myEnum;
@overrideWidgetbuild(BuildContext context) =>constMyScreen();
}
classMyEnumConverterextendsTypedGoRouteParameter<MyEnum> {
classMyEnumConverter(super.name);
@overrideString?toUrlString(MyEnum myEnum) {
// Return the correct format here.// If `null` is returned, fall back to the existing behavior.
}
@overrideMyEnum?fromUrlString(String value) {
// Return the correct value here.// If `null` is returned, fall back to the existing behavior.
}
}

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