Skip to content

Commit

Permalink
[go_router_builder] Support go_router v7 (#3858)
Browse files Browse the repository at this point in the history
#3819

Fixed go_router_builder to generate code for go_router v7.0.0.
  • Loading branch information
koji-1009 committed May 2, 2023
1 parent 0a12f2c commit 9c592e0
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 61 deletions.
5 changes: 5 additions & 0 deletions packages/go_router_builder/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
14 changes: 7 additions & 7 deletions packages/go_router_builder/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.

Expand All @@ -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);
},
),
Expand Down Expand Up @@ -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;
}
Expand All @@ -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 }
Expand Down
74 changes: 42 additions & 32 deletions packages/go_router_builder/example/lib/all_types.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions packages/go_router_builder/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions packages/go_router_builder/example/lib/main.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/go_router_builder/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions packages/go_router_builder/lib/src/type_helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)}'
Expand Down
4 changes: 2 additions & 2 deletions packages/go_router_builder/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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?,
);
Expand Down

0 comments on commit 9c592e0

Please sign in to comment.