Skip to content

Commit b017367

Browse files
authored
[go_router] Cleans up route match API and introduces dart fix (#3819)
Clean up API around RouteMatch/RouteMatchList/GoRouterState, This is a breaking change that renamed some of the GoRouterState property to have a more descriptive name as flutter style guide suggested https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo#avoid-abbreviations also introducing dart fix to help with migration
1 parent 2f95ecd commit b017367

40 files changed

+966
-448
lines changed

packages/go_router/CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
## 7.0.0
2+
3+
- **BREAKING CHANGE**:
4+
- For the below changes, run `dart fix --apply` to automatically migrate your code.
5+
- `GoRouteState.subloc` has been renamed to `GoRouteState.matchedLocation`.
6+
- `GoRouteState.params` has been renamed to `GoRouteState.pathParameters`.
7+
- `GoRouteState.fullpath` has been renamed to `GoRouteState.fullPath`.
8+
- `GoRouteState.queryParams` has been renamed to `GoRouteState.queryParameters`.
9+
- `params` and `queryParams` in `GoRouteState.namedLocation` have been renamed to `pathParameters` and `queryParameters`.
10+
- `params` and `queryParams` in `GoRouter`'s `namedLocation`, `pushNamed`, `pushReplacementNamed`
11+
`replaceNamed` have been renamed to `pathParameters` and `queryParameters`.
12+
- For the below changes, please follow the [migration guide](https://docs.google.com/document/d/10Xbpifbs4E-zh6YE5akIO8raJq_m3FIXs6nUGdOspOg).
13+
- `params` and `queryParams` in `BuildContext`'s `namedLocation`, `pushNamed`, `pushReplacementNamed`
14+
`replaceNamed` have been renamed to `pathParameters` and `queryParameters`.
15+
- Cleans up API and makes RouteMatchList immutable.
16+
117
## 6.5.9
218

319
- Removes navigator keys from `GoRouteData` and `ShellRouteData`.

packages/go_router/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ See the API documentation for details on the following topics:
3737
- [Error handling](https://pub.dev/documentation/go_router/latest/topics/Error%20handling-topic.html)
3838

3939
## Migration guides
40+
- [Migrating to 7.0.0](https://docs.google.com/document/d/10Xbpifbs4E-zh6YE5akIO8raJq_m3FIXs6nUGdOspOg).
4041
- [Migrating to 6.0.0](https://flutter.dev/go/go-router-v6-breaking-changes)
4142
- [Migrating to 5.1.2](https://flutter.dev/go/go-router-v5-1-2-breaking-changes)
4243
- [Migrating to 5.0](https://flutter.dev/go/go-router-v5-breaking-changes)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
include: ../../analysis_options.yaml
2+
3+
analyzer:
4+
exclude:
5+
- "test_fixes/**"

packages/go_router/doc/configuration.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ the builder callback:
4343
```dart
4444
GoRoute(
4545
path: '/users/:userId',
46-
builder: (context, state) => const UserScreen(id: state.params['userId']),
46+
builder: (context, state) => const UserScreen(id: state.pathParameters['userId']),
4747
),
4848
```
4949

@@ -55,7 +55,7 @@ after the `?`), use [GoRouterState][]. For example, a URL path such as
5555
```dart
5656
GoRoute(
5757
path: '/users',
58-
builder: (context, state) => const UsersScreen(filter: state.queryParams['filter']),
58+
builder: (context, state) => const UsersScreen(filter: state.queryParameters['filter']),
5959
),
6060
```
6161

packages/go_router/doc/named-routes.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ To navigate to a route using its name, call [`goNamed`](https://pub.dev/document
1414
```dart
1515
TextButton(
1616
onPressed: () {
17-
context.goNamed('song', params: {'songId': 123});
17+
context.goNamed('song', pathParameters: {'songId': 123});
1818
},
1919
child: const Text('Go to song 2'),
2020
),
@@ -25,7 +25,7 @@ Alternatively, you can look up the location for a name using `namedLocation`:
2525
```dart
2626
TextButton(
2727
onPressed: () {
28-
final String location = context.namedLocation('song', params: {'songId': 123});
28+
final String location = context.namedLocation('song', pathParameters: {'songId': 123});
2929
context.go(location);
3030
},
3131
child: const Text('Go to song 2'),

packages/go_router/example/lib/async_redirection.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class App extends StatelessWidget {
5555
// cause go_router to reparse current route if StreamAuth has new sign-in
5656
// information.
5757
final bool loggedIn = await StreamAuthScope.of(context).isSignedIn();
58-
final bool loggingIn = state.subloc == '/login';
58+
final bool loggingIn = state.matchedLocation == '/login';
5959
if (!loggedIn) {
6060
return '/login';
6161
}

packages/go_router/example/lib/books/main.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class Bookstore extends StatelessWidget {
6363
GoRoute(
6464
path: '/book/:bookId',
6565
redirect: (BuildContext context, GoRouterState state) =>
66-
'/books/all/${state.params['bookId']}',
66+
'/books/all/${state.pathParameters['bookId']}',
6767
),
6868
GoRoute(
6969
path: '/books/:kind(new|all|popular)',
@@ -72,14 +72,14 @@ class Bookstore extends StatelessWidget {
7272
key: _scaffoldKey,
7373
child: BookstoreScaffold(
7474
selectedTab: ScaffoldTab.books,
75-
child: BooksScreen(state.params['kind']!),
75+
child: BooksScreen(state.pathParameters['kind']!),
7676
),
7777
),
7878
routes: <GoRoute>[
7979
GoRoute(
8080
path: ':bookId',
8181
builder: (BuildContext context, GoRouterState state) {
82-
final String bookId = state.params['bookId']!;
82+
final String bookId = state.pathParameters['bookId']!;
8383
final Book? selectedBook = libraryInstance.allBooks
8484
.firstWhereOrNull((Book b) => b.id.toString() == bookId);
8585

@@ -91,7 +91,7 @@ class Bookstore extends StatelessWidget {
9191
GoRoute(
9292
path: '/author/:authorId',
9393
redirect: (BuildContext context, GoRouterState state) =>
94-
'/authors/${state.params['authorId']}',
94+
'/authors/${state.pathParameters['authorId']}',
9595
),
9696
GoRoute(
9797
path: '/authors',
@@ -107,7 +107,7 @@ class Bookstore extends StatelessWidget {
107107
GoRoute(
108108
path: ':authorId',
109109
builder: (BuildContext context, GoRouterState state) {
110-
final int authorId = int.parse(state.params['authorId']!);
110+
final int authorId = int.parse(state.pathParameters['authorId']!);
111111
final Author? selectedAuthor = libraryInstance.allAuthors
112112
.firstWhereOrNull((Author a) => a.id == authorId);
113113

@@ -135,7 +135,7 @@ class Bookstore extends StatelessWidget {
135135

136136
String? _guard(BuildContext context, GoRouterState state) {
137137
final bool signedIn = _auth.signedIn;
138-
final bool signingIn = state.subloc == '/signin';
138+
final bool signingIn = state.matchedLocation == '/signin';
139139

140140
// Go to /signin if the user is not signed in
141141
if (!signedIn && !signingIn) {

packages/go_router/example/lib/named_routes.dart

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,15 @@ class App extends StatelessWidget {
8484
name: 'family',
8585
path: 'family/:fid',
8686
builder: (BuildContext context, GoRouterState state) =>
87-
FamilyScreen(fid: state.params['fid']!),
87+
FamilyScreen(fid: state.pathParameters['fid']!),
8888
routes: <GoRoute>[
8989
GoRoute(
9090
name: 'person',
9191
path: 'person/:pid',
9292
builder: (BuildContext context, GoRouterState state) {
9393
return PersonScreen(
94-
fid: state.params['fid']!, pid: state.params['pid']!);
94+
fid: state.pathParameters['fid']!,
95+
pid: state.pathParameters['pid']!);
9596
},
9697
),
9798
],
@@ -119,7 +120,7 @@ class HomeScreen extends StatelessWidget {
119120
ListTile(
120121
title: Text(entry.value.name),
121122
onTap: () => context.go(context.namedLocation('family',
122-
params: <String, String>{'fid': entry.key})),
123+
pathParameters: <String, String>{'fid': entry.key})),
123124
)
124125
],
125126
),
@@ -147,8 +148,8 @@ class FamilyScreen extends StatelessWidget {
147148
title: Text(entry.value.name),
148149
onTap: () => context.go(context.namedLocation(
149150
'person',
150-
params: <String, String>{'fid': fid, 'pid': entry.key},
151-
queryParams: <String, String>{'qid': 'quid'},
151+
pathParameters: <String, String>{'fid': fid, 'pid': entry.key},
152+
queryParameters: <String, String>{'qid': 'quid'},
152153
)),
153154
),
154155
],

packages/go_router/example/lib/others/nav_observer.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ class Page1Screen extends StatelessWidget {
108108
ElevatedButton(
109109
onPressed: () => context.goNamed(
110110
'page2',
111-
params: <String, String>{'p1': 'pv1'},
112-
queryParams: <String, String>{'q1': 'qv1'},
111+
pathParameters: <String, String>{'p1': 'pv1'},
112+
queryParameters: <String, String>{'q1': 'qv1'},
113113
),
114114
child: const Text('Go to page 2'),
115115
),
@@ -134,7 +134,7 @@ class Page2Screen extends StatelessWidget {
134134
ElevatedButton(
135135
onPressed: () => context.goNamed(
136136
'page3',
137-
params: <String, String>{'p1': 'pv2'},
137+
pathParameters: <String, String>{'p1': 'pv2'},
138138
),
139139
child: const Text('Go to page 3'),
140140
),

packages/go_router/example/lib/others/push.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class App extends StatelessWidget {
3232
path: '/page2',
3333
builder: (BuildContext context, GoRouterState state) =>
3434
Page2ScreenWithPush(
35-
int.parse(state.queryParams['push-count']!),
35+
int.parse(state.queryParameters['push-count']!),
3636
),
3737
),
3838
],

packages/go_router/example/lib/path_and_query_parameters.dart

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import 'package:go_router/go_router.dart';
99
//
1010
// The route segments that start with ':' are treated as path parameters when
1111
// defining GoRoute[s]. The parameter values can be accessed through
12-
// GoRouterState.params.
12+
// GoRouterState.pathParameters.
1313
//
14-
// The query parameters are automatically stored in GoRouterState.queryParams.
14+
// The query parameters are automatically stored in GoRouterState.queryParameters.
1515

1616
/// Family data class.
1717
class Family {
@@ -84,8 +84,8 @@ class App extends StatelessWidget {
8484
path: 'family/:fid',
8585
builder: (BuildContext context, GoRouterState state) {
8686
return FamilyScreen(
87-
fid: state.params['fid']!,
88-
asc: state.queryParams['sort'] == 'asc',
87+
fid: state.pathParameters['fid']!,
88+
asc: state.queryParameters['sort'] == 'asc',
8989
);
9090
}),
9191
],
@@ -149,7 +149,8 @@ class FamilyScreen extends StatelessWidget {
149149
actions: <Widget>[
150150
IconButton(
151151
onPressed: () => context.goNamed('family',
152-
params: <String, String>{'fid': fid}, queryParams: newQueries),
152+
pathParameters: <String, String>{'fid': fid},
153+
queryParameters: newQueries),
153154
tooltip: 'sort ascending or descending',
154155
icon: const Icon(Icons.sort),
155156
)

packages/go_router/example/lib/redirection.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class App extends StatelessWidget {
7575
redirect: (BuildContext context, GoRouterState state) {
7676
// if the user is not logged in, they need to login
7777
final bool loggedIn = _loginInfo.loggedIn;
78-
final bool loggingIn = state.subloc == '/login';
78+
final bool loggingIn = state.matchedLocation == '/login';
7979
if (!loggedIn) {
8080
return '/login';
8181
}

packages/go_router/lib/fix_data.yaml

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
# Copyright 2014 The Flutter Authors. All rights reserved.
2+
# Use of this source code is governed by a BSD-style license that can be
3+
# found in the LICENSE file.
4+
5+
# For details regarding the *Flutter Fix* feature, see
6+
# https://flutter.dev/docs/development/tools/flutter-fix
7+
8+
# Please add new fixes to the top of the file, separated by one blank line
9+
# from other fixes. In a comment, include a link to the PR where the change
10+
# requiring the fix was made.
11+
12+
# Every fix must be tested. See the flutter/packages/flutter/test_fixes/README.md
13+
# file for instructions on testing these data driven fixes.
14+
15+
# For documentation about this file format, see
16+
# https://dart.dev/go/data-driven-fixes
17+
18+
version: 1
19+
transforms:
20+
- title: "Replaces 'params' and 'queryParams' in 'GoRouter.replaceNamed' with `pathParameters` and `queryParameters`"
21+
date: 2023-04-24
22+
bulkApply: true
23+
element:
24+
uris: [ 'go_router.dart' ]
25+
method: 'replaceNamed'
26+
inClass: 'GoRouter'
27+
changes:
28+
- kind: 'renameParameter'
29+
oldName: 'params'
30+
newName: 'pathParameters'
31+
- kind: 'renameParameter'
32+
oldName: 'queryParams'
33+
newName: 'queryParameters'
34+
- title: "Replaces 'params' and 'queryParams' in 'GoRouter.pushReplacementNamed' with `pathParameters` and `queryParameters`"
35+
date: 2023-04-24
36+
bulkApply: true
37+
element:
38+
uris: [ 'go_router.dart' ]
39+
method: 'pushReplacementNamed'
40+
inClass: 'GoRouter'
41+
changes:
42+
- kind: 'renameParameter'
43+
oldName: 'params'
44+
newName: 'pathParameters'
45+
- kind: 'renameParameter'
46+
oldName: 'queryParams'
47+
newName: 'queryParameters'
48+
49+
- title: "Replaces 'params' and 'queryParams' in 'GoRouter.pushNamed' with `pathParameters` and `queryParameters`"
50+
date: 2023-04-24
51+
bulkApply: true
52+
element:
53+
uris: [ 'go_router.dart' ]
54+
method: 'pushNamed'
55+
inClass: 'GoRouter'
56+
changes:
57+
- kind: 'renameParameter'
58+
oldName: 'params'
59+
newName: 'pathParameters'
60+
- kind: 'renameParameter'
61+
oldName: 'queryParams'
62+
newName: 'queryParameters'
63+
64+
- title: "Replaces 'params' and 'queryParams' in 'GoRouter.goNamed' with `pathParameters` and `queryParameters`"
65+
date: 2023-04-24
66+
bulkApply: true
67+
element:
68+
uris: [ 'go_router.dart' ]
69+
method: 'goNamed'
70+
inClass: 'GoRouter'
71+
changes:
72+
- kind: 'renameParameter'
73+
oldName: 'params'
74+
newName: 'pathParameters'
75+
- kind: 'renameParameter'
76+
oldName: 'queryParams'
77+
newName: 'queryParameters'
78+
79+
- title: "Replaces 'params' and 'queryParams' in 'GoRouter.namedLocation' with `pathParameters` and `queryParameters`"
80+
date: 2023-04-24
81+
bulkApply: true
82+
element:
83+
uris: [ 'go_router.dart' ]
84+
method: 'namedLocation'
85+
inClass: 'GoRouter'
86+
changes:
87+
- kind: 'renameParameter'
88+
oldName: 'params'
89+
newName: 'pathParameters'
90+
- kind: 'renameParameter'
91+
oldName: 'queryParams'
92+
newName: 'queryParameters'
93+
94+
- title: "Replaces 'params' and 'queryParams' in 'GoRouterState.namedLocation' with `pathParameters` and `queryParameters`"
95+
date: 2023-04-24
96+
bulkApply: true
97+
element:
98+
uris: [ 'go_router.dart' ]
99+
method: 'namedLocation'
100+
inClass: 'GoRouterState'
101+
changes:
102+
- kind: 'renameParameter'
103+
oldName: 'params'
104+
newName: 'pathParameters'
105+
- kind: 'renameParameter'
106+
oldName: 'queryParams'
107+
newName: 'queryParameters'
108+
109+
- title: "Replaces 'GoRouterState.queryParams' with 'GoRouterState.queryParameters'"
110+
date: 2023-04-24
111+
bulkApply: true
112+
element:
113+
uris: [ 'go_router.dart' ]
114+
field: 'queryParams'
115+
inClass: 'GoRouterState'
116+
changes:
117+
- kind: 'rename'
118+
newName: 'queryParameters'
119+
120+
- title: "Replaces 'GoRouterState.fullpath' with 'GoRouterState.fullPath'"
121+
date: 2023-04-24
122+
bulkApply: true
123+
element:
124+
uris: [ 'go_router.dart' ]
125+
field: 'fullpath'
126+
inClass: 'GoRouterState'
127+
changes:
128+
- kind: 'rename'
129+
newName: 'fullPath'
130+
131+
- title: "Replaces 'GoRouterState.params' with 'GoRouterState.pathParameters'"
132+
date: 2023-04-24
133+
bulkApply: true
134+
element:
135+
uris: [ 'go_router.dart' ]
136+
field: 'params'
137+
inClass: 'GoRouterState'
138+
changes:
139+
- kind: 'rename'
140+
newName: 'pathParameters'
141+
142+
- title: "Replaces 'GoRouterState.subloc' with 'GoRouterState.matchedLocation'"
143+
date: 2023-04-24
144+
bulkApply: true
145+
element:
146+
uris: [ 'go_router.dart' ]
147+
field: 'subloc'
148+
inClass: 'GoRouterState'
149+
changes:
150+
- kind: 'rename'
151+
newName: 'matchedLocation'

0 commit comments

Comments
 (0)