[go_router_builder] support v10.0.0 go_router (#4578)

fixes:131189
This commit is contained in:
hangyu
2023-07-28 15:09:00 -07:00
committed by GitHub
parent 441895b7b1
commit 7e536d79cf
16 changed files with 110 additions and 94 deletions

View File

@ -1,3 +1,7 @@
## 2.2.4
* Bumps example go_router version to v10.0.0 and migrate example code.
## 2.2.3
* Removes `path_to_regexp` from the dependencies.

View File

@ -58,7 +58,7 @@ class BigIntRoute extends GoRouteData {
Widget drawerTile(BuildContext context) => ListTile(
title: const Text('BigIntRoute'),
onTap: () => go(context),
selected: GoRouterState.of(context).location == location,
selected: GoRouterState.of(context).uri.toString() == location,
);
}
@ -84,7 +84,7 @@ class BoolRoute extends GoRouteData {
Widget drawerTile(BuildContext context) => ListTile(
title: const Text('BoolRoute'),
onTap: () => go(context),
selected: GoRouterState.of(context).location == location,
selected: GoRouterState.of(context).uri.toString() == location,
);
}
@ -107,7 +107,7 @@ class DateTimeRoute extends GoRouteData {
Widget drawerTile(BuildContext context) => ListTile(
title: const Text('DateTimeRoute'),
onTap: () => go(context),
selected: GoRouterState.of(context).location == location,
selected: GoRouterState.of(context).uri.toString() == location,
);
}
@ -133,7 +133,7 @@ class DoubleRoute extends GoRouteData {
Widget drawerTile(BuildContext context) => ListTile(
title: const Text('DoubleRoute'),
onTap: () => go(context),
selected: GoRouterState.of(context).location == location,
selected: GoRouterState.of(context).uri.toString() == location,
);
}
@ -159,7 +159,7 @@ class IntRoute extends GoRouteData {
Widget drawerTile(BuildContext context) => ListTile(
title: const Text('IntRoute'),
onTap: () => go(context),
selected: GoRouterState.of(context).location == location,
selected: GoRouterState.of(context).uri.toString() == location,
);
}
@ -185,7 +185,7 @@ class NumRoute extends GoRouteData {
Widget drawerTile(BuildContext context) => ListTile(
title: const Text('NumRoute'),
onTap: () => go(context),
selected: GoRouterState.of(context).location == location,
selected: GoRouterState.of(context).uri.toString() == location,
);
}
@ -212,7 +212,7 @@ class EnumRoute extends GoRouteData {
Widget drawerTile(BuildContext context) => ListTile(
title: const Text('EnumRoute'),
onTap: () => go(context),
selected: GoRouterState.of(context).location == location,
selected: GoRouterState.of(context).uri.toString() == location,
);
}
@ -239,7 +239,7 @@ class EnhancedEnumRoute extends GoRouteData {
Widget drawerTile(BuildContext context) => ListTile(
title: const Text('EnhancedEnumRoute'),
onTap: () => go(context),
selected: GoRouterState.of(context).location == location,
selected: GoRouterState.of(context).uri.toString() == location,
);
}
@ -265,7 +265,7 @@ class StringRoute extends GoRouteData {
Widget drawerTile(BuildContext context) => ListTile(
title: const Text('StringRoute'),
onTap: () => go(context),
selected: GoRouterState.of(context).location == location,
selected: GoRouterState.of(context).uri.toString() == location,
);
}
@ -288,7 +288,7 @@ class UriRoute extends GoRouteData {
Widget drawerTile(BuildContext context) => ListTile(
title: const Text('UriRoute'),
onTap: () => go(context),
selected: GoRouterState.of(context).location == location,
selected: GoRouterState.of(context).uri.toString() == location,
);
}
@ -361,7 +361,7 @@ class IterableRoute extends GoRouteData {
Widget drawerTile(BuildContext context) => ListTile(
title: const Text('IterableRoute'),
onTap: () => go(context),
selected: GoRouterState.of(context).location == location,
selected: GoRouterState.of(context).uri.toString() == location,
);
}
@ -430,7 +430,7 @@ class IterableRouteWithDefaultValues extends GoRouteData {
Widget drawerTile(BuildContext context) => ListTile(
title: const Text('IterableRouteWithDefaultValues'),
onTap: () => go(context),
selected: GoRouterState.of(context).location == location,
selected: GoRouterState.of(context).uri.toString() == location,
);
}
@ -536,7 +536,7 @@ class BasePage<T> extends StatelessWidget {
Text(
'Query param with default value: $queryParamWithDefaultValue',
),
SelectableText(GoRouterState.of(context).location),
SelectableText(GoRouterState.of(context).uri.toString()),
],
),
),

View File

@ -94,7 +94,7 @@ extension $BigIntRouteExtension on BigIntRoute {
requiredBigIntField:
BigInt.parse(state.pathParameters['requiredBigIntField']!),
bigIntField: _$convertMapValue(
'big-int-field', state.queryParameters, BigInt.parse),
'big-int-field', state.uri.queryParameters, BigInt.parse),
);
String get location => GoRouteData.$location(
@ -119,10 +119,10 @@ extension $BoolRouteExtension on BoolRoute {
requiredBoolField:
_$boolConverter(state.pathParameters['requiredBoolField']!),
boolField: _$convertMapValue(
'bool-field', state.queryParameters, _$boolConverter),
'bool-field', state.uri.queryParameters, _$boolConverter),
boolFieldWithDefaultValue: _$convertMapValue(
'bool-field-with-default-value',
state.queryParameters,
state.uri.queryParameters,
_$boolConverter) ??
true,
);
@ -152,7 +152,7 @@ extension $DateTimeRouteExtension on DateTimeRoute {
requiredDateTimeField:
DateTime.parse(state.pathParameters['requiredDateTimeField']!),
dateTimeField: _$convertMapValue(
'date-time-field', state.queryParameters, DateTime.parse),
'date-time-field', state.uri.queryParameters, DateTime.parse),
);
String get location => GoRouteData.$location(
@ -178,10 +178,10 @@ extension $DoubleRouteExtension on DoubleRoute {
requiredDoubleField:
double.parse(state.pathParameters['requiredDoubleField']!),
doubleField: _$convertMapValue(
'double-field', state.queryParameters, double.parse),
'double-field', state.uri.queryParameters, double.parse),
doubleFieldWithDefaultValue: _$convertMapValue(
'double-field-with-default-value',
state.queryParameters,
state.uri.queryParameters,
double.parse) ??
1.0,
);
@ -209,11 +209,11 @@ extension $DoubleRouteExtension on DoubleRoute {
extension $IntRouteExtension on IntRoute {
static IntRoute _fromState(GoRouterState state) => IntRoute(
requiredIntField: int.parse(state.pathParameters['requiredIntField']!),
intField:
_$convertMapValue('int-field', state.queryParameters, int.parse),
intField: _$convertMapValue(
'int-field', state.uri.queryParameters, int.parse),
intFieldWithDefaultValue: _$convertMapValue(
'int-field-with-default-value',
state.queryParameters,
state.uri.queryParameters,
int.parse) ??
1,
);
@ -240,11 +240,11 @@ extension $IntRouteExtension on IntRoute {
extension $NumRouteExtension on NumRoute {
static NumRoute _fromState(GoRouterState state) => NumRoute(
requiredNumField: num.parse(state.pathParameters['requiredNumField']!),
numField:
_$convertMapValue('num-field', state.queryParameters, num.parse),
numField: _$convertMapValue(
'num-field', state.uri.queryParameters, num.parse),
numFieldWithDefaultValue: _$convertMapValue(
'num-field-with-default-value',
state.queryParameters,
state.uri.queryParameters,
num.parse) ??
1,
);
@ -272,11 +272,11 @@ extension $EnumRouteExtension on EnumRoute {
static EnumRoute _fromState(GoRouterState state) => EnumRoute(
requiredEnumField: _$PersonDetailsEnumMap
._$fromName(state.pathParameters['requiredEnumField']!),
enumField: _$convertMapValue('enum-field', state.queryParameters,
enumField: _$convertMapValue('enum-field', state.uri.queryParameters,
_$PersonDetailsEnumMap._$fromName),
enumFieldWithDefaultValue: _$convertMapValue(
'enum-field-with-default-value',
state.queryParameters,
state.uri.queryParameters,
_$PersonDetailsEnumMap._$fromName) ??
PersonDetails.favoriteFood,
);
@ -312,11 +312,11 @@ extension $EnhancedEnumRouteExtension on EnhancedEnumRoute {
static EnhancedEnumRoute _fromState(GoRouterState state) => EnhancedEnumRoute(
requiredEnumField: _$SportDetailsEnumMap
._$fromName(state.pathParameters['requiredEnumField']!),
enumField: _$convertMapValue('enum-field', state.queryParameters,
enumField: _$convertMapValue('enum-field', state.uri.queryParameters,
_$SportDetailsEnumMap._$fromName),
enumFieldWithDefaultValue: _$convertMapValue(
'enum-field-with-default-value',
state.queryParameters,
state.uri.queryParameters,
_$SportDetailsEnumMap._$fromName) ??
SportDetails.football,
);
@ -352,9 +352,9 @@ const _$SportDetailsEnumMap = {
extension $StringRouteExtension on StringRoute {
static StringRoute _fromState(GoRouterState state) => StringRoute(
requiredStringField: state.pathParameters['requiredStringField']!,
stringField: state.queryParameters['string-field'],
stringField: state.uri.queryParameters['string-field'],
stringFieldWithDefaultValue:
state.queryParameters['string-field-with-default-value'] ??
state.uri.queryParameters['string-field-with-default-value'] ??
'defaultValue',
);
@ -380,8 +380,8 @@ extension $StringRouteExtension on StringRoute {
extension $UriRouteExtension on UriRoute {
static UriRoute _fromState(GoRouterState state) => UriRoute(
requiredUriField: Uri.parse(state.pathParameters['requiredUriField']!),
uriField:
_$convertMapValue('uri-field', state.queryParameters, Uri.parse),
uriField: _$convertMapValue(
'uri-field', state.uri.queryParameters, Uri.parse),
);
String get location => GoRouteData.$location(
@ -404,49 +404,56 @@ extension $UriRouteExtension on UriRoute {
extension $IterableRouteExtension on IterableRoute {
static IterableRoute _fromState(GoRouterState state) => IterableRoute(
intIterableField:
state.queryParametersAll['int-iterable-field']?.map(int.parse),
doubleIterableField: state.queryParametersAll['double-iterable-field']
state.uri.queryParametersAll['int-iterable-field']?.map(int.parse),
doubleIterableField: state
.uri.queryParametersAll['double-iterable-field']
?.map(double.parse),
stringIterableField:
state.queryParametersAll['string-iterable-field']?.map((e) => e),
boolIterableField: state.queryParametersAll['bool-iterable-field']
stringIterableField: state
.uri.queryParametersAll['string-iterable-field']
?.map((e) => e),
boolIterableField: state.uri.queryParametersAll['bool-iterable-field']
?.map(_$boolConverter),
enumIterableField: state.queryParametersAll['enum-iterable-field']
enumIterableField: state.uri.queryParametersAll['enum-iterable-field']
?.map(_$SportDetailsEnumMap._$fromName),
enumOnlyInIterableField: state
.queryParametersAll['enum-only-in-iterable-field']
.uri.queryParametersAll['enum-only-in-iterable-field']
?.map(_$CookingRecipeEnumMap._$fromName),
intListField:
state.queryParametersAll['int-list-field']?.map(int.parse).toList(),
doubleListField: state.queryParametersAll['double-list-field']
intListField: state.uri.queryParametersAll['int-list-field']
?.map(int.parse)
.toList(),
doubleListField: state.uri.queryParametersAll['double-list-field']
?.map(double.parse)
.toList(),
stringListField: state.queryParametersAll['string-list-field']
stringListField: state.uri.queryParametersAll['string-list-field']
?.map((e) => e)
.toList(),
boolListField: state.queryParametersAll['bool-list-field']
boolListField: state.uri.queryParametersAll['bool-list-field']
?.map(_$boolConverter)
.toList(),
enumListField: state.queryParametersAll['enum-list-field']
enumListField: state.uri.queryParametersAll['enum-list-field']
?.map(_$SportDetailsEnumMap._$fromName)
.toList(),
enumOnlyInListField: state.queryParametersAll['enum-only-in-list-field']
enumOnlyInListField: state
.uri.queryParametersAll['enum-only-in-list-field']
?.map(_$CookingRecipeEnumMap._$fromName)
.toList(),
intSetField:
state.queryParametersAll['int-set-field']?.map(int.parse).toSet(),
doubleSetField: state.queryParametersAll['double-set-field']
intSetField: state.uri.queryParametersAll['int-set-field']
?.map(int.parse)
.toSet(),
doubleSetField: state.uri.queryParametersAll['double-set-field']
?.map(double.parse)
.toSet(),
stringSetField:
state.queryParametersAll['string-set-field']?.map((e) => e).toSet(),
boolSetField: state.queryParametersAll['bool-set-field']
stringSetField: state.uri.queryParametersAll['string-set-field']
?.map((e) => e)
.toSet(),
boolSetField: state.uri.queryParametersAll['bool-set-field']
?.map(_$boolConverter)
.toSet(),
enumSetField: state.queryParametersAll['enum-set-field']
enumSetField: state.uri.queryParametersAll['enum-set-field']
?.map(_$SportDetailsEnumMap._$fromName)
.toSet(),
enumOnlyInSetField: state.queryParametersAll['enum-only-in-set-field']
enumOnlyInSetField: state
.uri.queryParametersAll['enum-only-in-set-field']
?.map(_$CookingRecipeEnumMap._$fromName)
.toSet(),
);
@ -529,57 +536,60 @@ extension $IterableRouteWithDefaultValuesExtension
on IterableRouteWithDefaultValues {
static IterableRouteWithDefaultValues _fromState(GoRouterState state) =>
IterableRouteWithDefaultValues(
intIterableField:
state.queryParametersAll['int-iterable-field']?.map(int.parse) ??
const <int>[0],
doubleIterableField: state.queryParametersAll['double-iterable-field']
intIterableField: state.uri.queryParametersAll['int-iterable-field']
?.map(int.parse) ??
const <int>[0],
doubleIterableField: state
.uri.queryParametersAll['double-iterable-field']
?.map(double.parse) ??
const <double>[0, 1, 2],
stringIterableField:
state.queryParametersAll['string-iterable-field']?.map((e) => e) ??
const <String>['defaultValue'],
boolIterableField: state.queryParametersAll['bool-iterable-field']
stringIterableField: state
.uri.queryParametersAll['string-iterable-field']
?.map((e) => e) ??
const <String>['defaultValue'],
boolIterableField: state.uri.queryParametersAll['bool-iterable-field']
?.map(_$boolConverter) ??
const <bool>[false],
enumIterableField: state.queryParametersAll['enum-iterable-field']
enumIterableField: state.uri.queryParametersAll['enum-iterable-field']
?.map(_$SportDetailsEnumMap._$fromName) ??
const <SportDetails>[SportDetails.tennis, SportDetails.hockey],
intListField: state.queryParametersAll['int-list-field']
intListField: state.uri.queryParametersAll['int-list-field']
?.map(int.parse)
.toList() ??
const <int>[0],
doubleListField: state.queryParametersAll['double-list-field']
doubleListField: state.uri.queryParametersAll['double-list-field']
?.map(double.parse)
.toList() ??
const <double>[1, 2, 3],
stringListField: state.queryParametersAll['string-list-field']
stringListField: state.uri.queryParametersAll['string-list-field']
?.map((e) => e)
.toList() ??
const <String>['defaultValue0', 'defaultValue1'],
boolListField: state.queryParametersAll['bool-list-field']
boolListField: state.uri.queryParametersAll['bool-list-field']
?.map(_$boolConverter)
.toList() ??
const <bool>[true],
enumListField: state.queryParametersAll['enum-list-field']
enumListField: state.uri.queryParametersAll['enum-list-field']
?.map(_$SportDetailsEnumMap._$fromName)
.toList() ??
const <SportDetails>[SportDetails.football],
intSetField:
state.queryParametersAll['int-set-field']?.map(int.parse).toSet() ??
const <int>{0, 1},
doubleSetField: state.queryParametersAll['double-set-field']
intSetField: state.uri.queryParametersAll['int-set-field']
?.map(int.parse)
.toSet() ??
const <int>{0, 1},
doubleSetField: state.uri.queryParametersAll['double-set-field']
?.map(double.parse)
.toSet() ??
const <double>{},
stringSetField: state.queryParametersAll['string-set-field']
stringSetField: state.uri.queryParametersAll['string-set-field']
?.map((e) => e)
.toSet() ??
const <String>{'defaultValue'},
boolSetField: state.queryParametersAll['bool-set-field']
boolSetField: state.uri.queryParametersAll['bool-set-field']
?.map(_$boolConverter)
.toSet() ??
const <bool>{true, false},
enumSetField: state.queryParametersAll['enum-set-field']
enumSetField: state.uri.queryParametersAll['enum-set-field']
?.map(_$SportDetailsEnumMap._$fromName)
.toSet() ??
const <SportDetails>{SportDetails.hockey},

View File

@ -158,7 +158,7 @@ RouteBase get $loginRoute => GoRouteData.$route(
extension $LoginRouteExtension on LoginRoute {
static LoginRoute _fromState(GoRouterState state) => LoginRoute(
fromPage: state.queryParameters['from-page'],
fromPage: state.uri.queryParameters['from-page'],
);
String get location => GoRouteData.$location(

View File

@ -77,7 +77,7 @@ class MyShellRouteScreen extends StatelessWidget {
final Widget child;
int getCurrentIndex(BuildContext context) {
final String location = GoRouterState.of(context).location;
final String location = GoRouterState.of(context).uri.toString();
if (location == '/bar') {
return 1;
}

View File

@ -57,7 +57,7 @@ class MyShellRouteScreen extends StatelessWidget {
final Widget child;
int getCurrentIndex(BuildContext context) {
final String location = GoRouterState.of(context).location;
final String location = GoRouterState.of(context).uri.toString();
if (location.startsWith('/users')) {
return 1;
}

View File

@ -8,7 +8,7 @@ environment:
dependencies:
flutter:
sdk: flutter
go_router: ^9.0.3
go_router: ^10.0.0
provider: 6.0.5
dev_dependencies:

View File

@ -101,7 +101,7 @@ String _stateValueAccess(ParameterElement element, Set<String> pathParameters) {
if (pathParameters.contains(element.name)) {
access = 'pathParameters[${escapeDartString(element.name)}]';
} else {
access = 'queryParameters[${escapeDartString(element.name.kebab)}]';
access = 'uri.queryParameters[${escapeDartString(element.name.kebab)}]';
}
if (pathParameters.contains(element.name) ||
(!element.type.isNullableType && !element.hasDefaultValue)) {
@ -284,12 +284,12 @@ class _TypeHelperIterable extends _TypeHelper {
}
return '''
state.queryParametersAll[
state.uri.queryParametersAll[
${escapeDartString(parameterElement.name.kebab)}]
?.map($entriesTypeDecoder)$iterableCaster''';
}
return '''
state.queryParametersAll[${escapeDartString(parameterElement.name.kebab)}]''';
state.uri.queryParametersAll[${escapeDartString(parameterElement.name.kebab)}]''';
}
@override
@ -334,7 +334,7 @@ abstract class _TypeHelperWithHelper extends _TypeHelper {
(paramType.isNullableType || parameterElement.hasDefaultValue)) {
return '$convertMapValueHelperName('
'${escapeDartString(parameterName.kebab)}, '
'state.queryParameters, '
'state.uri.queryParameters, '
'${helperName(paramType)})';
}
return '${helperName(paramType)}'

View File

@ -2,7 +2,7 @@ name: go_router_builder
description: >-
A builder that supports generated strongly-typed route helpers for
package:go_router
version: 2.2.3
version: 2.2.4
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
@ -23,5 +23,5 @@ dependencies:
dev_dependencies:
build_test: ^2.1.7
dart_style: 2.3.2
go_router: ^9.0.0
go_router: ^10.0.0
test: ^1.20.0

View File

@ -6,7 +6,8 @@ RouteBase get $defaultValueRoute => GoRouteData.$route(
extension $DefaultValueRouteExtension on DefaultValueRoute {
static DefaultValueRoute _fromState(GoRouterState state) => DefaultValueRoute(
param:
_$convertMapValue('param', state.queryParameters, int.parse) ?? 0,
_$convertMapValue('param', state.uri.queryParameters, int.parse) ??
0,
);
String get location => GoRouteData.$location(

View File

@ -6,7 +6,8 @@ RouteBase get $extraValueRoute => GoRouteData.$route(
extension $ExtraValueRouteExtension on ExtraValueRoute {
static ExtraValueRoute _fromState(GoRouterState state) => ExtraValueRoute(
param:
_$convertMapValue('param', state.queryParameters, int.parse) ?? 0,
_$convertMapValue('param', state.uri.queryParameters, int.parse) ??
0,
$extra: state.extra as int?,
);

View File

@ -6,8 +6,8 @@ RouteBase get $iterableDefaultValueRoute => GoRouteData.$route(
extension $IterableDefaultValueRouteExtension on IterableDefaultValueRoute {
static IterableDefaultValueRoute _fromState(GoRouterState state) =>
IterableDefaultValueRoute(
param:
state.queryParametersAll['param']?.map(int.parse) ?? const <int>[0],
param: state.uri.queryParametersAll['param']?.map(int.parse) ??
const <int>[0],
);
String get location => GoRouteData.$location(

View File

@ -6,7 +6,7 @@ RouteBase get $iterableWithEnumRoute => GoRouteData.$route(
extension $IterableWithEnumRouteExtension on IterableWithEnumRoute {
static IterableWithEnumRoute _fromState(GoRouterState state) =>
IterableWithEnumRoute(
param: state.queryParametersAll['param']
param: state.uri.queryParametersAll['param']
?.map(_$EnumOnlyUsedInIterableEnumMap._$fromName),
);

View File

@ -7,7 +7,7 @@ extension $NullableRequiredParamNotInPathExtension
on NullableRequiredParamNotInPath {
static NullableRequiredParamNotInPath _fromState(GoRouterState state) =>
NullableRequiredParamNotInPath(
id: _$convertMapValue('id', state.queryParameters, int.parse),
id: _$convertMapValue('id', state.uri.queryParameters, int.parse),
);
String get location => GoRouteData.$location(

View File

@ -7,7 +7,7 @@ extension $NonNullableRequiredParamNotInPathExtension
on NonNullableRequiredParamNotInPath {
static NonNullableRequiredParamNotInPath _fromState(GoRouterState state) =>
NonNullableRequiredParamNotInPath(
id: int.parse(state.queryParameters['id']!),
id: int.parse(state.uri.queryParameters['id']!),
);
String get location => GoRouteData.$location(

View File

@ -56,6 +56,6 @@ Future<void> main() async {
.trim()
.replaceAll('\r\n', '\n');
expect(generated, equals(expectResult.replaceAll('\r\n', '\n')));
});
}, timeout: const Timeout(Duration(seconds: 100)));
}
}