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
],

0 commit comments

Comments
 (0)