-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[go_router_builder] Support custom types #11068
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
32e416b
fa3293f
255391d
afb6dbf
1c321e9
035b6df
316447d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,35 @@ part 'typed_query_parameter_example.g.dart'; | |
|
|
||
| void main() => runApp(App()); | ||
|
|
||
| class CustomParameter { | ||
| const CustomParameter({required this.valueString, required this.valueInt}); | ||
|
|
||
| final String valueString; | ||
| final int valueInt; | ||
|
|
||
| static String? encode(CustomParameter? parameter) { | ||
| if (parameter == null) { | ||
| return null; | ||
| } | ||
| return '${parameter.valueString},${parameter.valueInt}'; | ||
| } | ||
|
|
||
| static CustomParameter? decode(String? value) { | ||
| if (value == null) { | ||
| return null; | ||
| } | ||
| final List<String> parts = value.split(','); | ||
| return CustomParameter( | ||
| valueString: parts[0], | ||
| valueInt: int.parse(parts[1]), | ||
| ); | ||
| } | ||
|
|
||
| static bool compare(CustomParameter a, CustomParameter b) { | ||
| return a.valueString != b.valueString || a.valueInt != b.valueInt; | ||
| } | ||
| } | ||
|
|
||
| class App extends StatelessWidget { | ||
| App({super.key}); | ||
|
|
||
|
|
@@ -27,34 +56,56 @@ class App extends StatelessWidget { | |
| @TypedGoRoute<IntRoute>(path: '/int-route') | ||
| class IntRoute extends GoRouteData with $IntRoute { | ||
| IntRoute({ | ||
| @TypedQueryParameter(name: 'intField') this.intField, | ||
| @TypedQueryParameter(name: 'int_field_with_default_value') | ||
| @TypedQueryParameter<int>(name: 'intField') this.intField, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seems like this may be a breaking change?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I considered it a non-breaking change because existing usages of But yes, ultimately, if the linter rules What do you think? Should we flag it as a breaking change, and is it an acceptable one? Or is it okay to not flag it as a breaking change?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you decorate the class TypedQueryParameter with |
||
| @TypedQueryParameter<int>(name: 'int_field_with_default_value') | ||
| this.intFieldWithDefaultValue = 1, | ||
| @TypedQueryParameter(name: 'int field') this.intFieldWithSpace, | ||
| @TypedQueryParameter<int>(name: 'int field') this.intFieldWithSpace, | ||
| @TypedQueryParameter<CustomParameter>( | ||
| encoder: CustomParameter.encode, | ||
| decoder: CustomParameter.decode, | ||
| ) | ||
| this.customField, | ||
| @TypedQueryParameter<CustomParameter>( | ||
| encoder: CustomParameter.encode, | ||
| decoder: CustomParameter.decode, | ||
| compare: CustomParameter.compare, | ||
| ) | ||
| this.customFieldWithDefaultValue = const CustomParameter( | ||
| valueString: 'default', | ||
| valueInt: 0, | ||
| ), | ||
| }); | ||
|
|
||
| final int? intField; | ||
| final int intFieldWithDefaultValue; | ||
| final int? intFieldWithSpace; | ||
| final CustomParameter? customField; | ||
| final CustomParameter customFieldWithDefaultValue; | ||
| @override | ||
| Widget build(BuildContext context, GoRouterState state) => Screen( | ||
| intField: intField, | ||
| intFieldWithDefaultValue: intFieldWithDefaultValue, | ||
| intFieldWithSpace: intFieldWithSpace, | ||
| customField: customField, | ||
| customFieldWithDefaultValue: customFieldWithDefaultValue, | ||
| ); | ||
| } | ||
|
|
||
| class Screen extends StatelessWidget { | ||
| const Screen({ | ||
| super.key, | ||
| required this.intField, | ||
| required this.intFieldWithDefaultValue, | ||
| this.intFieldWithSpace, | ||
| super.key, | ||
| this.customField, | ||
| required this.customFieldWithDefaultValue, | ||
| }); | ||
|
|
||
| final int? intField; | ||
| final int intFieldWithDefaultValue; | ||
| final int? intFieldWithSpace; | ||
| final CustomParameter? customField; | ||
| final CustomParameter customFieldWithDefaultValue; | ||
|
|
||
| @override | ||
| Widget build(BuildContext context) => Scaffold( | ||
|
|
@@ -75,6 +126,8 @@ class Screen extends StatelessWidget { | |
| intField: newValue, | ||
| intFieldWithDefaultValue: intFieldWithDefaultValue, | ||
| intFieldWithSpace: intFieldWithSpace, | ||
| customField: customField, | ||
| customFieldWithDefaultValue: customFieldWithDefaultValue, | ||
| ).go(context); | ||
| }, | ||
| ), | ||
|
|
@@ -88,6 +141,8 @@ class Screen extends StatelessWidget { | |
| intField: intField, | ||
| intFieldWithDefaultValue: newValue, | ||
| intFieldWithSpace: intFieldWithSpace, | ||
| customField: customField, | ||
| customFieldWithDefaultValue: customFieldWithDefaultValue, | ||
| ).go(context); | ||
| }, | ||
| ), | ||
|
|
@@ -101,6 +156,46 @@ class Screen extends StatelessWidget { | |
| intField: intField, | ||
| intFieldWithDefaultValue: intFieldWithDefaultValue, | ||
| intFieldWithSpace: newValue, | ||
| customField: customField, | ||
| customFieldWithDefaultValue: customFieldWithDefaultValue, | ||
| ).go(context); | ||
| }, | ||
| ), | ||
| ListTile( | ||
| title: const Text('customField:'), | ||
| subtitle: Text(CustomParameter.encode(customField) ?? ''), | ||
| trailing: const Icon(Icons.add), | ||
| onTap: () { | ||
| final newValue = CustomParameter( | ||
| valueString: '${customField?.valueString ?? ''}-', | ||
| valueInt: (customField?.valueInt ?? 0) + 1, | ||
| ); | ||
| IntRoute( | ||
| intField: intField, | ||
| intFieldWithDefaultValue: intFieldWithDefaultValue, | ||
| intFieldWithSpace: intFieldWithSpace, | ||
| customField: newValue, | ||
| customFieldWithDefaultValue: customFieldWithDefaultValue, | ||
| ).go(context); | ||
| }, | ||
| ), | ||
| ListTile( | ||
| title: const Text('customFieldWithDefaultValue:'), | ||
| subtitle: Text( | ||
| CustomParameter.encode(customFieldWithDefaultValue)!, | ||
| ), | ||
| trailing: const Icon(Icons.add), | ||
| onTap: () { | ||
| final newValue = CustomParameter( | ||
| valueString: '${customFieldWithDefaultValue.valueString}-', | ||
| valueInt: customFieldWithDefaultValue.valueInt + 1, | ||
| ); | ||
| IntRoute( | ||
| intField: intField, | ||
| intFieldWithDefaultValue: intFieldWithDefaultValue, | ||
| intFieldWithSpace: intFieldWithSpace, | ||
| customField: customField, | ||
| customFieldWithDefaultValue: newValue, | ||
| ).go(context); | ||
| }, | ||
| ), | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
decodemethod is not robust against malformed input and could lead to runtime exceptions. For example, if thevaluestring doesn't contain a comma, accessingparts[1]will throw aRangeError. If the second part is not a valid integer,int.parsewill throw aFormatException.Since this is an example file, it's a good opportunity to demonstrate best practices for parsing. Consider adding checks for the number of parts and using
int.tryParsefor safer integer conversion.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is true. But that's only an example. So I'd think the current code is okay. Let me know if you think otherwise