[go_router_builder]Update the way to pass the `extra` param in router_config (#3005)
[go_router_builder]Update the way to pass the `extra` param in router_config
diff --git a/packages/go_router_builder/CHANGELOG.md b/packages/go_router_builder/CHANGELOG.md
index 8f30c0c..6f6b6ed 100644
--- a/packages/go_router_builder/CHANGELOG.md
+++ b/packages/go_router_builder/CHANGELOG.md
@@ -1,3 +1,7 @@
+## 1.1.3
+
+* Updates router_config to not passing itself as `extra`.
+
## 1.1.2
* Adds support for Iterables, Lists and Sets in query params for TypedGoRoute. [#108437](https://github.com/flutter/flutter/issues/108437).
diff --git a/packages/go_router_builder/example/lib/all_types.g.dart b/packages/go_router_builder/example/lib/all_types.g.dart
index c5ab5cf..61f38b7 100644
--- a/packages/go_router_builder/example/lib/all_types.g.dart
+++ b/packages/go_router_builder/example/lib/all_types.g.dart
@@ -75,12 +75,12 @@
'/',
);
- void go(BuildContext context) => context.go(location, extra: this);
+ void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location, extra: this);
+ void push(BuildContext context) => context.push(location);
void pushReplacement(BuildContext context) =>
- context.pushReplacement(location, extra: this);
+ context.pushReplacement(location);
}
extension $BigIntRouteExtension on BigIntRoute {
@@ -97,12 +97,12 @@
},
);
- void go(BuildContext context) => context.go(location, extra: this);
+ void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location, extra: this);
+ void push(BuildContext context) => context.push(location);
void pushReplacement(BuildContext context) =>
- context.pushReplacement(location, extra: this);
+ context.pushReplacement(location);
}
extension $BoolRouteExtension on BoolRoute {
@@ -127,12 +127,12 @@
},
);
- void go(BuildContext context) => context.go(location, extra: this);
+ void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location, extra: this);
+ void push(BuildContext context) => context.push(location);
void pushReplacement(BuildContext context) =>
- context.pushReplacement(location, extra: this);
+ context.pushReplacement(location);
}
extension $DateTimeRouteExtension on DateTimeRoute {
@@ -151,12 +151,12 @@
},
);
- void go(BuildContext context) => context.go(location, extra: this);
+ void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location, extra: this);
+ void push(BuildContext context) => context.push(location);
void pushReplacement(BuildContext context) =>
- context.pushReplacement(location, extra: this);
+ context.pushReplacement(location);
}
extension $DoubleRouteExtension on DoubleRoute {
@@ -181,12 +181,12 @@
},
);
- void go(BuildContext context) => context.go(location, extra: this);
+ void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location, extra: this);
+ void push(BuildContext context) => context.push(location);
void pushReplacement(BuildContext context) =>
- context.pushReplacement(location, extra: this);
+ context.pushReplacement(location);
}
extension $IntRouteExtension on IntRoute {
@@ -207,12 +207,12 @@
},
);
- void go(BuildContext context) => context.go(location, extra: this);
+ void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location, extra: this);
+ void push(BuildContext context) => context.push(location);
void pushReplacement(BuildContext context) =>
- context.pushReplacement(location, extra: this);
+ context.pushReplacement(location);
}
extension $NumRouteExtension on NumRoute {
@@ -233,12 +233,12 @@
},
);
- void go(BuildContext context) => context.go(location, extra: this);
+ void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location, extra: this);
+ void push(BuildContext context) => context.push(location);
void pushReplacement(BuildContext context) =>
- context.pushReplacement(location, extra: this);
+ context.pushReplacement(location);
}
extension $EnumRouteExtension on EnumRoute {
@@ -265,12 +265,12 @@
},
);
- void go(BuildContext context) => context.go(location, extra: this);
+ void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location, extra: this);
+ void push(BuildContext context) => context.push(location);
void pushReplacement(BuildContext context) =>
- context.pushReplacement(location, extra: this);
+ context.pushReplacement(location);
}
extension $EnhancedEnumRouteExtension on EnhancedEnumRoute {
@@ -297,12 +297,12 @@
},
);
- void go(BuildContext context) => context.go(location, extra: this);
+ void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location, extra: this);
+ void push(BuildContext context) => context.push(location);
void pushReplacement(BuildContext context) =>
- context.pushReplacement(location, extra: this);
+ context.pushReplacement(location);
}
extension $StringRouteExtension on StringRoute {
@@ -323,12 +323,12 @@
},
);
- void go(BuildContext context) => context.go(location, extra: this);
+ void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location, extra: this);
+ void push(BuildContext context) => context.push(location);
void pushReplacement(BuildContext context) =>
- context.pushReplacement(location, extra: this);
+ context.pushReplacement(location);
}
extension $UriRouteExtension on UriRoute {
@@ -344,12 +344,12 @@
},
);
- void go(BuildContext context) => context.go(location, extra: this);
+ void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location, extra: this);
+ void push(BuildContext context) => context.push(location);
void pushReplacement(BuildContext context) =>
- context.pushReplacement(location, extra: this);
+ context.pushReplacement(location);
}
extension $IterableRouteExtension on IterableRoute {
@@ -439,12 +439,12 @@
},
);
- void go(BuildContext context) => context.go(location, extra: this);
+ void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location, extra: this);
+ void push(BuildContext context) => context.push(location);
void pushReplacement(BuildContext context) =>
- context.pushReplacement(location, extra: this);
+ context.pushReplacement(location);
}
const _$PersonDetailsEnumMap = {
diff --git a/packages/go_router_builder/example/lib/main.g.dart b/packages/go_router_builder/example/lib/main.g.dart
index 6f61344..ff7a236 100644
--- a/packages/go_router_builder/example/lib/main.g.dart
+++ b/packages/go_router_builder/example/lib/main.g.dart
@@ -43,12 +43,12 @@
'/',
);
- void go(BuildContext context) => context.go(location, extra: this);
+ void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location, extra: this);
+ void push(BuildContext context) => context.push(location);
void pushReplacement(BuildContext context) =>
- context.pushReplacement(location, extra: this);
+ context.pushReplacement(location);
}
extension $FamilyRouteExtension on FamilyRoute {
@@ -60,12 +60,12 @@
'/family/${Uri.encodeComponent(fid)}',
);
- void go(BuildContext context) => context.go(location, extra: this);
+ void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location, extra: this);
+ void push(BuildContext context) => context.push(location);
void pushReplacement(BuildContext context) =>
- context.pushReplacement(location, extra: this);
+ context.pushReplacement(location);
}
extension $PersonRouteExtension on PersonRoute {
@@ -78,12 +78,12 @@
'/family/${Uri.encodeComponent(fid)}/person/${Uri.encodeComponent(pid.toString())}',
);
- void go(BuildContext context) => context.go(location, extra: this);
+ void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location, extra: this);
+ void push(BuildContext context) => context.push(location);
void pushReplacement(BuildContext context) =>
- context.pushReplacement(location, extra: this);
+ context.pushReplacement(location);
}
extension $PersonDetailsRouteExtension on PersonDetailsRoute {
@@ -99,12 +99,12 @@
'/family/${Uri.encodeComponent(fid)}/person/${Uri.encodeComponent(pid.toString())}/details/${Uri.encodeComponent(_$PersonDetailsEnumMap[details]!)}',
);
- void go(BuildContext context) => context.go(location, extra: this);
+ void go(BuildContext context) => context.go(location, extra: $extra);
- void push(BuildContext context) => context.push(location, extra: this);
+ void push(BuildContext context) => context.push(location, extra: $extra);
void pushReplacement(BuildContext context) =>
- context.pushReplacement(location, extra: this);
+ context.pushReplacement(location, extra: $extra);
}
const _$PersonDetailsEnumMap = {
@@ -135,10 +135,10 @@
},
);
- void go(BuildContext context) => context.go(location, extra: this);
+ void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location, extra: this);
+ void push(BuildContext context) => context.push(location);
void pushReplacement(BuildContext context) =>
- context.pushReplacement(location, extra: this);
+ context.pushReplacement(location);
}
diff --git a/packages/go_router_builder/example/lib/simple_example.g.dart b/packages/go_router_builder/example/lib/simple_example.g.dart
index a71180d..6f9eb6a 100644
--- a/packages/go_router_builder/example/lib/simple_example.g.dart
+++ b/packages/go_router_builder/example/lib/simple_example.g.dart
@@ -30,12 +30,12 @@
'/',
);
- void go(BuildContext context) => context.go(location, extra: this);
+ void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location, extra: this);
+ void push(BuildContext context) => context.push(location);
void pushReplacement(BuildContext context) =>
- context.pushReplacement(location, extra: this);
+ context.pushReplacement(location);
}
extension $FamilyRouteExtension on FamilyRoute {
@@ -47,10 +47,10 @@
'/family/${Uri.encodeComponent(familyId)}',
);
- void go(BuildContext context) => context.go(location, extra: this);
+ void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location, extra: this);
+ void push(BuildContext context) => context.push(location);
void pushReplacement(BuildContext context) =>
- context.pushReplacement(location, extra: this);
+ context.pushReplacement(location);
}
diff --git a/packages/go_router_builder/lib/src/route_config.dart b/packages/go_router_builder/lib/src/route_config.dart
index f957734..87087a3 100644
--- a/packages/go_router_builder/lib/src/route_config.dart
+++ b/packages/go_router_builder/lib/src/route_config.dart
@@ -142,12 +142,14 @@
String get location => GoRouteData.\$location($_locationArgs,$_locationQueryParams);
- void go(BuildContext context) => context.go(location, extra: this);
+ void go(BuildContext context) =>
+ context.go(location${_extraParam != null ? ', extra: $extraFieldName' : ''});
- void push(BuildContext context) => context.push(location, extra: this);
+ void push(BuildContext context) =>
+ context.push(location${_extraParam != null ? ', extra: $extraFieldName' : ''});
void pushReplacement(BuildContext context) =>
- context.pushReplacement(location, extra: this);
+ context.pushReplacement(location${_extraParam != null ? ', extra: $extraFieldName' : ''});
}
''';
@@ -188,14 +190,16 @@
}
}
+ ParameterElement? get _extraParam => _ctor.parameters
+ .singleWhereOrNull((ParameterElement element) => element.isExtraField);
+
String get _newFromState {
final StringBuffer buffer = StringBuffer('=>');
if (_ctor.isConst && _ctorParams.isEmpty && _ctorQueryParams.isEmpty) {
buffer.writeln('const ');
}
- final ParameterElement? extraParam = _ctor.parameters
- .singleWhereOrNull((ParameterElement element) => element.isExtraField);
+ final ParameterElement? extraParam = _extraParam;
buffer.writeln('$_className(');
for (final ParameterElement param in <ParameterElement>[
diff --git a/packages/go_router_builder/pubspec.yaml b/packages/go_router_builder/pubspec.yaml
index 1eead91..a5fb5ae 100644
--- a/packages/go_router_builder/pubspec.yaml
+++ b/packages/go_router_builder/pubspec.yaml
@@ -2,7 +2,7 @@
description: >-
A builder that supports generated strongly-typed route helpers for
package:go_router
-version: 1.1.2
+version: 1.1.3
repository: https://github.com/flutter/packages/tree/main/packages/go_router_builder
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+go_router_builder%22
diff --git a/packages/go_router_builder/test/builder_test.dart b/packages/go_router_builder/test/builder_test.dart
index 644c13f..68c5a1b 100644
--- a/packages/go_router_builder/test/builder_test.dart
+++ b/packages/go_router_builder/test/builder_test.dart
@@ -25,6 +25,7 @@
'AppliedToWrongClassType',
'BadPathParam',
'ExtraMustBeOptional',
+ 'ExtraValueRoute',
'MissingPathParam',
'MissingPathValue',
'MissingTypeAnnotation',
diff --git a/packages/go_router_builder/test/test_inputs/_go_router_builder_test_input.dart b/packages/go_router_builder/test/test_inputs/_go_router_builder_test_input.dart
index c85e8e3..abb47ad 100644
--- a/packages/go_router_builder/test/test_inputs/_go_router_builder_test_input.dart
+++ b/packages/go_router_builder/test/test_inputs/_go_router_builder_test_input.dart
@@ -86,12 +86,12 @@
'/${Uri.encodeComponent(_$EnumTestEnumMap[y]!)}',
);
- void go(BuildContext context) => context.go(location, extra: this);
+ void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location, extra: this);
+ void push(BuildContext context) => context.push(location);
void pushReplacement(BuildContext context) =>
- context.pushReplacement(location, extra: this);
+ context.pushReplacement(location);
}
const _$EnumTestEnumMap = {
@@ -138,12 +138,12 @@
},
);
- void go(BuildContext context) => context.go(location, extra: this);
+ void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location, extra: this);
+ void push(BuildContext context) => context.push(location);
void pushReplacement(BuildContext context) =>
- context.pushReplacement(location, extra: this);
+ context.pushReplacement(location);
}
T? _$convertMapValue<T>(
@@ -161,6 +161,49 @@
final int param;
}
+@ShouldGenerate(r'''
+GoRoute get $extraValueRoute => GoRouteData.$route(
+ path: '/default-value-route',
+ factory: $ExtraValueRouteExtension._fromState,
+ );
+
+extension $ExtraValueRouteExtension on ExtraValueRoute {
+ static ExtraValueRoute _fromState(GoRouterState state) => ExtraValueRoute(
+ param: _$convertMapValue('param', state.queryParams, int.parse) ?? 0,
+ $extra: state.extra as int?,
+ );
+
+ String get location => GoRouteData.$location(
+ '/default-value-route',
+ queryParams: {
+ if (param != 0) 'param': param.toString(),
+ },
+ );
+
+ void go(BuildContext context) => context.go(location, extra: $extra);
+
+ void push(BuildContext context) => context.push(location, extra: $extra);
+
+ void pushReplacement(BuildContext context) =>
+ context.pushReplacement(location, extra: $extra);
+}
+
+T? _$convertMapValue<T>(
+ String key,
+ Map<String, String> map,
+ T Function(String) converter,
+) {
+ final value = map[key];
+ return value == null ? null : converter(value);
+}
+''')
+@TypedGoRoute<ExtraValueRoute>(path: '/default-value-route')
+class ExtraValueRoute extends GoRouteData {
+ ExtraValueRoute({this.param = 0, this.$extra});
+ final int param;
+ final int? $extra;
+}
+
@ShouldThrow(
'Default value used with a nullable type. Only non-nullable type can have a default value.',
todo: 'Remove the default value or make the type non-nullable.',