From 9c592e0ac230814dc6e2991888235601a62bdb2d Mon Sep 17 00:00:00 2001 From: Koji Wakamiya Date: Wed, 3 May 2023 01:52:50 +0900 Subject: [PATCH] [go_router_builder] Support go_router v7 (#3858) https://github.com/flutter/packages/pull/3819 Fixed go_router_builder to generate code for go_router v7.0.0. --- packages/go_router_builder/CHANGELOG.md | 5 ++ packages/go_router_builder/README.md | 14 ++-- .../example/lib/all_types.g.dart | 74 +++++++++++-------- .../go_router_builder/example/lib/main.dart | 6 +- .../go_router_builder/example/lib/main.g.dart | 16 ++-- .../lib/shell_route_with_keys_example.g.dart | 2 +- .../example/lib/simple_example.g.dart | 2 +- .../go_router_builder/example/pubspec.yaml | 2 +- .../lib/src/type_helpers.dart | 6 +- packages/go_router_builder/pubspec.yaml | 4 +- .../_go_router_builder_test_input.dart | 8 +- 11 files changed, 78 insertions(+), 61 deletions(-) diff --git a/packages/go_router_builder/CHANGELOG.md b/packages/go_router_builder/CHANGELOG.md index a7414a840e5..304b579e8ab 100644 --- a/packages/go_router_builder/CHANGELOG.md +++ b/packages/go_router_builder/CHANGELOG.md @@ -1,3 +1,8 @@ +## 2.0.0 + +* Updates the documentation to go_router v7.0.0. +* Bumps go_router version in example folder to v7.0.0. + ## 1.2.2 * Supports returning value in generated `push` method. [go_router CHANGELOG](https://github.com/flutter/packages/blob/main/packages/go_router/CHANGELOG.md#650) diff --git a/packages/go_router_builder/README.md b/packages/go_router_builder/README.md index 6632625afc8..aa1ef3349b6 100644 --- a/packages/go_router_builder/README.md +++ b/packages/go_router_builder/README.md @@ -8,12 +8,12 @@ To use `go_router_builder`, you need to have the following dependencies in ```yaml dependencies: # ...along with your other dependencies - go_router: ^3.1.0 + go_router: ^7.0.0 dev_dependencies: # ...along with your other dev-dependencies build_runner: ^2.0.0 - go_router_builder: ^1.0.0 + go_router_builder: ^2.0.0 ``` ### Source code @@ -46,7 +46,7 @@ Read more about using in a URI format into one or more page builders, each that require zero or more arguments that are passed as path and query parameters as part of the location. `go_router` does a good job of making the path and query parameters available -via the `params` and `queryParams` properties of the `GoRouterState` object, but +via the `pathParameters` and `queryParameters` properties of the `GoRouterState` object, but often the page builder must first parse the parameters into types that aren't `String`s, e.g. @@ -55,7 +55,7 @@ GoRoute( path: ':authorId', builder: (context, state) { // require the authorId to be present and be an integer - final authorId = int.parse(state.params['authorId']!); + final authorId = int.parse(state.pathParameters['authorId']!); return AuthorDetailsScreen(authorId: authorId); }, ), @@ -258,8 +258,8 @@ generator: ```dart redirect: (state) { final loggedIn = loginInfo.loggedIn; - final loggingIn = state.subloc == LoginRoute().location; - if( !loggedIn && !loggingIn ) return LoginRoute(from: state.subloc).location; + final loggingIn = state.matchedLocation == LoginRoute().location; + if( !loggedIn && !loggingIn ) return LoginRoute(from: state.matchedLocation).location; if( loggedIn && loggingIn ) return HomeRoute().location; return null; } @@ -280,7 +280,7 @@ class HomeRoute extends GoRouteData { ## Type conversions The code generator can convert simple types like `int` and `enum` to/from the -`String` type of the underlying params: +`String` type of the underlying pathParameters: ```dart enum BookKind { all, popular, recent } 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 7cf692f6cbe..8f3add393eb 100644 --- a/packages/go_router_builder/example/lib/all_types.g.dart +++ b/packages/go_router_builder/example/lib/all_types.g.dart @@ -89,9 +89,10 @@ extension $AllTypesBaseRouteExtension on AllTypesBaseRoute { extension $BigIntRouteExtension on BigIntRoute { static BigIntRoute _fromState(GoRouterState state) => BigIntRoute( - requiredBigIntField: BigInt.parse(state.params['requiredBigIntField']!), - bigIntField: - _$convertMapValue('big-int-field', state.queryParams, BigInt.parse), + requiredBigIntField: + BigInt.parse(state.pathParameters['requiredBigIntField']!), + bigIntField: _$convertMapValue( + 'big-int-field', state.queryParameters, BigInt.parse), ); String get location => GoRouteData.$location( @@ -111,12 +112,13 @@ extension $BigIntRouteExtension on BigIntRoute { extension $BoolRouteExtension on BoolRoute { static BoolRoute _fromState(GoRouterState state) => BoolRoute( - requiredBoolField: _$boolConverter(state.params['requiredBoolField']!), - boolField: - _$convertMapValue('bool-field', state.queryParams, _$boolConverter), + requiredBoolField: + _$boolConverter(state.pathParameters['requiredBoolField']!), + boolField: _$convertMapValue( + 'bool-field', state.queryParameters, _$boolConverter), boolFieldWithDefaultValue: _$convertMapValue( 'bool-field-with-default-value', - state.queryParams, + state.queryParameters, _$boolConverter) ?? true, ); @@ -142,9 +144,9 @@ extension $BoolRouteExtension on BoolRoute { extension $DateTimeRouteExtension on DateTimeRoute { static DateTimeRoute _fromState(GoRouterState state) => DateTimeRoute( requiredDateTimeField: - DateTime.parse(state.params['requiredDateTimeField']!), + DateTime.parse(state.pathParameters['requiredDateTimeField']!), dateTimeField: _$convertMapValue( - 'date-time-field', state.queryParams, DateTime.parse), + 'date-time-field', state.queryParameters, DateTime.parse), ); String get location => GoRouteData.$location( @@ -165,12 +167,13 @@ extension $DateTimeRouteExtension on DateTimeRoute { extension $DoubleRouteExtension on DoubleRoute { static DoubleRoute _fromState(GoRouterState state) => DoubleRoute( - requiredDoubleField: double.parse(state.params['requiredDoubleField']!), - doubleField: - _$convertMapValue('double-field', state.queryParams, double.parse), + requiredDoubleField: + double.parse(state.pathParameters['requiredDoubleField']!), + doubleField: _$convertMapValue( + 'double-field', state.queryParameters, double.parse), doubleFieldWithDefaultValue: _$convertMapValue( 'double-field-with-default-value', - state.queryParams, + state.queryParameters, double.parse) ?? 1.0, ); @@ -195,10 +198,13 @@ extension $DoubleRouteExtension on DoubleRoute { extension $IntRouteExtension on IntRoute { static IntRoute _fromState(GoRouterState state) => IntRoute( - requiredIntField: int.parse(state.params['requiredIntField']!), - intField: _$convertMapValue('int-field', state.queryParams, int.parse), + requiredIntField: int.parse(state.pathParameters['requiredIntField']!), + intField: + _$convertMapValue('int-field', state.queryParameters, int.parse), intFieldWithDefaultValue: _$convertMapValue( - 'int-field-with-default-value', state.queryParams, int.parse) ?? + 'int-field-with-default-value', + state.queryParameters, + int.parse) ?? 1, ); @@ -221,10 +227,13 @@ extension $IntRouteExtension on IntRoute { extension $NumRouteExtension on NumRoute { static NumRoute _fromState(GoRouterState state) => NumRoute( - requiredNumField: num.parse(state.params['requiredNumField']!), - numField: _$convertMapValue('num-field', state.queryParams, num.parse), + requiredNumField: num.parse(state.pathParameters['requiredNumField']!), + numField: + _$convertMapValue('num-field', state.queryParameters, num.parse), numFieldWithDefaultValue: _$convertMapValue( - 'num-field-with-default-value', state.queryParams, num.parse) ?? + 'num-field-with-default-value', + state.queryParameters, + num.parse) ?? 1, ); @@ -248,12 +257,12 @@ extension $NumRouteExtension on NumRoute { extension $EnumRouteExtension on EnumRoute { static EnumRoute _fromState(GoRouterState state) => EnumRoute( requiredEnumField: _$PersonDetailsEnumMap - ._$fromName(state.params['requiredEnumField']!), - enumField: _$convertMapValue( - 'enum-field', state.queryParams, _$PersonDetailsEnumMap._$fromName), + ._$fromName(state.pathParameters['requiredEnumField']!), + enumField: _$convertMapValue('enum-field', state.queryParameters, + _$PersonDetailsEnumMap._$fromName), enumFieldWithDefaultValue: _$convertMapValue( 'enum-field-with-default-value', - state.queryParams, + state.queryParameters, _$PersonDetailsEnumMap._$fromName) ?? PersonDetails.favoriteFood, ); @@ -280,12 +289,12 @@ extension $EnumRouteExtension on EnumRoute { extension $EnhancedEnumRouteExtension on EnhancedEnumRoute { static EnhancedEnumRoute _fromState(GoRouterState state) => EnhancedEnumRoute( requiredEnumField: _$SportDetailsEnumMap - ._$fromName(state.params['requiredEnumField']!), - enumField: _$convertMapValue( - 'enum-field', state.queryParams, _$SportDetailsEnumMap._$fromName), + ._$fromName(state.pathParameters['requiredEnumField']!), + enumField: _$convertMapValue('enum-field', state.queryParameters, + _$SportDetailsEnumMap._$fromName), enumFieldWithDefaultValue: _$convertMapValue( 'enum-field-with-default-value', - state.queryParams, + state.queryParameters, _$SportDetailsEnumMap._$fromName) ?? SportDetails.football, ); @@ -311,10 +320,10 @@ extension $EnhancedEnumRouteExtension on EnhancedEnumRoute { extension $StringRouteExtension on StringRoute { static StringRoute _fromState(GoRouterState state) => StringRoute( - requiredStringField: state.params['requiredStringField']!, - stringField: state.queryParams['string-field'], + requiredStringField: state.pathParameters['requiredStringField']!, + stringField: state.queryParameters['string-field'], stringFieldWithDefaultValue: - state.queryParams['string-field-with-default-value'] ?? + state.queryParameters['string-field-with-default-value'] ?? 'defaultValue', ); @@ -337,8 +346,9 @@ extension $StringRouteExtension on StringRoute { extension $UriRouteExtension on UriRoute { static UriRoute _fromState(GoRouterState state) => UriRoute( - requiredUriField: Uri.parse(state.params['requiredUriField']!), - uriField: _$convertMapValue('uri-field', state.queryParams, Uri.parse), + requiredUriField: Uri.parse(state.pathParameters['requiredUriField']!), + uriField: + _$convertMapValue('uri-field', state.queryParameters, Uri.parse), ); String get location => GoRouteData.$location( diff --git a/packages/go_router_builder/example/lib/main.dart b/packages/go_router_builder/example/lib/main.dart index cf917c59b5c..a8ff9ad5b6f 100644 --- a/packages/go_router_builder/example/lib/main.dart +++ b/packages/go_router_builder/example/lib/main.dart @@ -40,13 +40,13 @@ class App extends StatelessWidget { redirect: (BuildContext context, GoRouterState state) { final bool loggedIn = loginInfo.loggedIn; - // check just the subloc in case there are query parameters + // check just the matchedLocation in case there are query parameters final String loginLoc = const LoginRoute().location; - final bool goingToLogin = state.subloc == loginLoc; + final bool goingToLogin = state.matchedLocation == loginLoc; // the user is not logged in and not headed to /login, they need to login if (!loggedIn && !goingToLogin) { - return LoginRoute(fromPage: state.subloc).location; + return LoginRoute(fromPage: state.matchedLocation).location; } // the user is logged in and headed to /login, no need to login again diff --git a/packages/go_router_builder/example/lib/main.g.dart b/packages/go_router_builder/example/lib/main.g.dart index 864e8e121ef..031877e8890 100644 --- a/packages/go_router_builder/example/lib/main.g.dart +++ b/packages/go_router_builder/example/lib/main.g.dart @@ -57,7 +57,7 @@ extension $HomeRouteExtension on HomeRoute { extension $FamilyRouteExtension on FamilyRoute { static FamilyRoute _fromState(GoRouterState state) => FamilyRoute( - state.params['fid']!, + state.pathParameters['fid']!, ); String get location => GoRouteData.$location( @@ -74,8 +74,8 @@ extension $FamilyRouteExtension on FamilyRoute { extension $PersonRouteExtension on PersonRoute { static PersonRoute _fromState(GoRouterState state) => PersonRoute( - state.params['fid']!, - int.parse(state.params['pid']!), + state.pathParameters['fid']!, + int.parse(state.pathParameters['pid']!), ); String get location => GoRouteData.$location( @@ -93,9 +93,9 @@ extension $PersonRouteExtension on PersonRoute { extension $PersonDetailsRouteExtension on PersonDetailsRoute { static PersonDetailsRoute _fromState(GoRouterState state) => PersonDetailsRoute( - state.params['fid']!, - int.parse(state.params['pid']!), - _$PersonDetailsEnumMap._$fromName(state.params['details']!), + state.pathParameters['fid']!, + int.parse(state.pathParameters['pid']!), + _$PersonDetailsEnumMap._$fromName(state.pathParameters['details']!), $extra: state.extra as int?, ); @@ -114,7 +114,7 @@ extension $PersonDetailsRouteExtension on PersonDetailsRoute { extension $FamilyCountRouteExtension on FamilyCountRoute { static FamilyCountRoute _fromState(GoRouterState state) => FamilyCountRoute( - int.parse(state.params['count']!), + int.parse(state.pathParameters['count']!), ); String get location => GoRouteData.$location( @@ -147,7 +147,7 @@ RouteBase get $loginRoute => GoRouteData.$route( extension $LoginRouteExtension on LoginRoute { static LoginRoute _fromState(GoRouterState state) => LoginRoute( - fromPage: state.queryParams['from-page'], + fromPage: state.queryParameters['from-page'], ); String get location => GoRouteData.$location( diff --git a/packages/go_router_builder/example/lib/shell_route_with_keys_example.g.dart b/packages/go_router_builder/example/lib/shell_route_with_keys_example.g.dart index a8a51017e33..b34d274a1aa 100644 --- a/packages/go_router_builder/example/lib/shell_route_with_keys_example.g.dart +++ b/packages/go_router_builder/example/lib/shell_route_with_keys_example.g.dart @@ -72,7 +72,7 @@ extension $UsersRouteDataExtension on UsersRouteData { extension $UserRouteDataExtension on UserRouteData { static UserRouteData _fromState(GoRouterState state) => UserRouteData( - id: int.parse(state.params['id']!), + id: int.parse(state.pathParameters['id']!), ); String get location => GoRouteData.$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 16538ef1622..410c914bacb 100644 --- a/packages/go_router_builder/example/lib/simple_example.g.dart +++ b/packages/go_router_builder/example/lib/simple_example.g.dart @@ -40,7 +40,7 @@ extension $HomeRouteExtension on HomeRoute { extension $FamilyRouteExtension on FamilyRoute { static FamilyRoute _fromState(GoRouterState state) => FamilyRoute( - state.params['familyId']!, + state.pathParameters['familyId']!, ); String get location => GoRouteData.$location( diff --git a/packages/go_router_builder/example/pubspec.yaml b/packages/go_router_builder/example/pubspec.yaml index b8b7c9fa679..6942a4fa3af 100644 --- a/packages/go_router_builder/example/pubspec.yaml +++ b/packages/go_router_builder/example/pubspec.yaml @@ -8,7 +8,7 @@ environment: dependencies: flutter: sdk: flutter - go_router: ^6.2.0 + go_router: ^7.0.0 provider: 6.0.5 dev_dependencies: diff --git a/packages/go_router_builder/lib/src/type_helpers.dart b/packages/go_router_builder/lib/src/type_helpers.dart index 9f51e1b1576..1ed85072465 100644 --- a/packages/go_router_builder/lib/src/type_helpers.dart +++ b/packages/go_router_builder/lib/src/type_helpers.dart @@ -98,11 +98,11 @@ String _stateValueAccess(ParameterElement element) { } if (element.isRequired) { - return 'params[${escapeDartString(element.name)}]!'; + return 'pathParameters[${escapeDartString(element.name)}]!'; } if (element.isOptional) { - return 'queryParams[${escapeDartString(element.name.kebab)}]'; + return 'queryParameters[${escapeDartString(element.name.kebab)}]'; } throw InvalidGenerationSourceError( @@ -329,7 +329,7 @@ abstract class _TypeHelperWithHelper extends _TypeHelper { if (!parameterElement.isRequired) { return '$convertMapValueHelperName(' '${escapeDartString(parameterElement.name.kebab)}, ' - 'state.queryParams, ' + 'state.queryParameters, ' '${helperName(paramType)})'; } return '${helperName(paramType)}' diff --git a/packages/go_router_builder/pubspec.yaml b/packages/go_router_builder/pubspec.yaml index 11726b6ebcc..1f8b7b89f9b 100644 --- a/packages/go_router_builder/pubspec.yaml +++ b/packages/go_router_builder/pubspec.yaml @@ -2,7 +2,7 @@ name: go_router_builder description: >- A builder that supports generated strongly-typed route helpers for package:go_router -version: 1.2.2 +version: 2.0.0 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,6 +23,6 @@ dependencies: dev_dependencies: build_runner: ^2.0.0 - go_router: ^6.0.10 + go_router: ^7.0.0 source_gen_test: ^1.0.0 test: ^1.20.0 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 1e39c205ef6..39b4add4fb9 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 @@ -70,7 +70,7 @@ RouteBase get $enumParam => GoRouteData.$route( extension $EnumParamExtension on EnumParam { static EnumParam _fromState(GoRouterState state) => EnumParam( - y: _$EnumTestEnumMap._$fromName(state.params['y']!), + y: _$EnumTestEnumMap._$fromName(state.pathParameters['y']!), ); String get location => GoRouteData.$location( @@ -119,7 +119,8 @@ RouteBase get $defaultValueRoute => GoRouteData.$route( extension $DefaultValueRouteExtension on DefaultValueRoute { static DefaultValueRoute _fromState(GoRouterState state) => DefaultValueRoute( - param: _$convertMapValue('param', state.queryParams, int.parse) ?? 0, + param: + _$convertMapValue('param', state.queryParameters, int.parse) ?? 0, ); String get location => GoRouteData.$location( @@ -160,7 +161,8 @@ RouteBase get $extraValueRoute => GoRouteData.$route( extension $ExtraValueRouteExtension on ExtraValueRoute { static ExtraValueRoute _fromState(GoRouterState state) => ExtraValueRoute( - param: _$convertMapValue('param', state.queryParams, int.parse) ?? 0, + param: + _$convertMapValue('param', state.queryParameters, int.parse) ?? 0, $extra: state.extra as int?, );