Skip to content

Commit 488da36

Browse files
authored
Access current location using uri.path to support deep links (#6474)
This PR updates examples to use `uri.path` instead of `uri.toString()` for accessing the current location. While the examples don't use deep linking, promoting the usage of `uri.toString()` in the examples doesn't seem to be a good idea as it can lead to issues when it's used with deep links (It'll include host and scheme).
1 parent d8447f9 commit 488da36

12 files changed

+47
-38
lines changed

packages/go_router/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 13.2.4
2+
3+
- Updates examples to use uri.path instead of uri.toString() for accessing the current location.
4+
15
## 13.2.3
26

37
- Fixes an issue where deep links without path caused an exception

packages/go_router/example/lib/shell_route.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ class ScaffoldWithNavBar extends StatelessWidget {
152152
}
153153

154154
static int _calculateSelectedIndex(BuildContext context) {
155-
final String location = GoRouterState.of(context).uri.toString();
155+
final String location = GoRouterState.of(context).uri.path;
156156
if (location.startsWith('/a')) {
157157
return 0;
158158
}

packages/go_router/example/lib/shell_route_top_route.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ class ScaffoldWithNavBar extends StatelessWidget {
195195
}
196196

197197
static int _calculateSelectedIndex(BuildContext context) {
198-
final String location = GoRouterState.of(context).uri.toString();
198+
final String location = GoRouterState.of(context).uri.path;
199199
if (location.startsWith('/a')) {
200200
return 0;
201201
}

packages/go_router/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: go_router
22
description: A declarative router for Flutter based on Navigation 2 supporting
33
deep linking, data-driven routes and more
4-
version: 13.2.3
4+
version: 13.2.4
55
repository: https://github.com/flutter/packages/tree/main/packages/go_router
66
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+go_router%22
77

packages/go_router/test/go_router_state_test.dart

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,23 @@ void main() {
4242
path: '/',
4343
builder: (_, __) {
4444
return Builder(builder: (BuildContext context) {
45-
return Text('1 ${GoRouterState.of(context).uri}');
45+
return Text('1 ${GoRouterState.of(context).uri.path}');
4646
});
4747
},
4848
routes: <GoRoute>[
4949
GoRoute(
5050
path: 'a',
5151
builder: (_, __) {
5252
return Builder(builder: (BuildContext context) {
53-
return Text('2 ${GoRouterState.of(context).uri}');
53+
return Text('2 ${GoRouterState.of(context).uri.path}');
5454
});
5555
}),
5656
]),
5757
];
5858
final GoRouter router = await createRouter(routes, tester);
59-
router.go('/?p=123');
59+
router.go('/');
6060
await tester.pumpAndSettle();
61-
expect(find.text('1 /?p=123'), findsOneWidget);
61+
expect(find.text('1 /'), findsOneWidget);
6262

6363
router.go('/a');
6464
await tester.pumpAndSettle();
@@ -74,7 +74,7 @@ void main() {
7474
path: '/',
7575
builder: (_, __) {
7676
return Builder(builder: (BuildContext context) {
77-
return Text('1 ${GoRouterState.of(context).uri}');
77+
return Text('1 ${GoRouterState.of(context).uri.path}');
7878
});
7979
},
8080
routes: <GoRoute>[
@@ -110,23 +110,22 @@ void main() {
110110
path: '/',
111111
builder: (_, __) {
112112
return Builder(builder: (BuildContext context) {
113-
return Text(GoRouterState.of(context).uri.toString());
113+
return Text(GoRouterState.of(context).uri.path);
114114
});
115115
},
116116
routes: <GoRoute>[
117117
GoRoute(
118118
path: 'a',
119119
builder: (_, __) {
120120
return Builder(builder: (BuildContext context) {
121-
return Text(
122-
key: key, GoRouterState.of(context).uri.toString());
121+
return Text(key: key, GoRouterState.of(context).uri.path);
123122
});
124123
}),
125124
]),
126125
];
127126
final GoRouter router =
128-
await createRouter(routes, tester, initialLocation: '/a?p=123');
129-
expect(tester.widget<Text>(find.byKey(key)).data, '/a?p=123');
127+
await createRouter(routes, tester, initialLocation: '/a');
128+
expect(tester.widget<Text>(find.byKey(key)).data, '/a');
130129
final GoRouterStateRegistry registry = tester
131130
.widget<GoRouterStateRegistryScope>(
132131
find.byType(GoRouterStateRegistryScope))
@@ -136,7 +135,7 @@ void main() {
136135
await tester.pump();
137136
expect(registry.registry.length, 2);
138137
// should retain the same location even if the location has changed.
139-
expect(tester.widget<Text>(find.byKey(key)).data, '/a?p=123');
138+
expect(tester.widget<Text>(find.byKey(key)).data, '/a');
140139

141140
// Finish the pop animation.
142141
await tester.pumpAndSettle();
@@ -153,23 +152,22 @@ void main() {
153152
path: '/',
154153
builder: (_, __) {
155154
return Builder(builder: (BuildContext context) {
156-
return Text(GoRouterState.of(context).uri.toString());
155+
return Text(GoRouterState.of(context).uri.path);
157156
});
158157
},
159158
routes: <GoRoute>[
160159
GoRoute(
161160
path: 'a',
162161
builder: (_, __) {
163162
return Builder(builder: (BuildContext context) {
164-
return Text(
165-
key: key, GoRouterState.of(context).uri.toString());
163+
return Text(key: key, GoRouterState.of(context).uri.path);
166164
});
167165
}),
168166
]),
169167
];
170168
await createRouter(routes, tester,
171-
initialLocation: '/a?p=123', navigatorKey: nav);
172-
expect(tester.widget<Text>(find.byKey(key)).data, '/a?p=123');
169+
initialLocation: '/a', navigatorKey: nav);
170+
expect(tester.widget<Text>(find.byKey(key)).data, '/a');
173171
final GoRouterStateRegistry registry = tester
174172
.widget<GoRouterStateRegistryScope>(
175173
find.byType(GoRouterStateRegistryScope))
@@ -179,7 +177,7 @@ void main() {
179177
await tester.pump();
180178
expect(registry.registry.length, 2);
181179
// should retain the same location even if the location has changed.
182-
expect(tester.widget<Text>(find.byKey(key)).data, '/a?p=123');
180+
expect(tester.widget<Text>(find.byKey(key)).data, '/a');
183181

184182
// Finish the pop animation.
185183
await tester.pumpAndSettle();

packages/go_router_builder/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.5.1
2+
3+
- Updates examples to use uri.path instead of uri.toString() for accessing the current location.
4+
15
## 2.5.0
26

37
* Updates minimum supported SDK version to Flutter 3.13/Dart 3.1.

packages/go_router_builder/example/lib/all_types.dart

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class BigIntRoute extends GoRouteData {
5858
Widget drawerTile(BuildContext context) => ListTile(
5959
title: const Text('BigIntRoute'),
6060
onTap: () => go(context),
61-
selected: GoRouterState.of(context).uri.toString() == location,
61+
selected: GoRouterState.of(context).uri.path == location,
6262
);
6363
}
6464

@@ -84,7 +84,7 @@ class BoolRoute extends GoRouteData {
8484
Widget drawerTile(BuildContext context) => ListTile(
8585
title: const Text('BoolRoute'),
8686
onTap: () => go(context),
87-
selected: GoRouterState.of(context).uri.toString() == location,
87+
selected: GoRouterState.of(context).uri.path == location,
8888
);
8989
}
9090

@@ -107,7 +107,7 @@ class DateTimeRoute extends GoRouteData {
107107
Widget drawerTile(BuildContext context) => ListTile(
108108
title: const Text('DateTimeRoute'),
109109
onTap: () => go(context),
110-
selected: GoRouterState.of(context).uri.toString() == location,
110+
selected: GoRouterState.of(context).uri.path == location,
111111
);
112112
}
113113

@@ -133,7 +133,7 @@ class DoubleRoute extends GoRouteData {
133133
Widget drawerTile(BuildContext context) => ListTile(
134134
title: const Text('DoubleRoute'),
135135
onTap: () => go(context),
136-
selected: GoRouterState.of(context).uri.toString() == location,
136+
selected: GoRouterState.of(context).uri.path == location,
137137
);
138138
}
139139

@@ -159,7 +159,7 @@ class IntRoute extends GoRouteData {
159159
Widget drawerTile(BuildContext context) => ListTile(
160160
title: const Text('IntRoute'),
161161
onTap: () => go(context),
162-
selected: GoRouterState.of(context).uri.toString() == location,
162+
selected: GoRouterState.of(context).uri.path == location,
163163
);
164164
}
165165

@@ -185,7 +185,7 @@ class NumRoute extends GoRouteData {
185185
Widget drawerTile(BuildContext context) => ListTile(
186186
title: const Text('NumRoute'),
187187
onTap: () => go(context),
188-
selected: GoRouterState.of(context).uri.toString() == location,
188+
selected: GoRouterState.of(context).uri.path == location,
189189
);
190190
}
191191

@@ -212,7 +212,7 @@ class EnumRoute extends GoRouteData {
212212
Widget drawerTile(BuildContext context) => ListTile(
213213
title: const Text('EnumRoute'),
214214
onTap: () => go(context),
215-
selected: GoRouterState.of(context).uri.toString() == location,
215+
selected: GoRouterState.of(context).uri.path == location,
216216
);
217217
}
218218

@@ -239,7 +239,7 @@ class EnhancedEnumRoute extends GoRouteData {
239239
Widget drawerTile(BuildContext context) => ListTile(
240240
title: const Text('EnhancedEnumRoute'),
241241
onTap: () => go(context),
242-
selected: GoRouterState.of(context).uri.toString() == location,
242+
selected: GoRouterState.of(context).uri.path == location,
243243
);
244244
}
245245

@@ -265,7 +265,7 @@ class StringRoute extends GoRouteData {
265265
Widget drawerTile(BuildContext context) => ListTile(
266266
title: const Text('StringRoute'),
267267
onTap: () => go(context),
268-
selected: GoRouterState.of(context).uri.toString() == location,
268+
selected: GoRouterState.of(context).uri.path == location,
269269
);
270270
}
271271

@@ -288,7 +288,7 @@ class UriRoute extends GoRouteData {
288288
Widget drawerTile(BuildContext context) => ListTile(
289289
title: const Text('UriRoute'),
290290
onTap: () => go(context),
291-
selected: GoRouterState.of(context).uri.toString() == location,
291+
selected: GoRouterState.of(context).uri.path == location,
292292
);
293293
}
294294

@@ -361,7 +361,7 @@ class IterableRoute extends GoRouteData {
361361
Widget drawerTile(BuildContext context) => ListTile(
362362
title: const Text('IterableRoute'),
363363
onTap: () => go(context),
364-
selected: GoRouterState.of(context).uri.toString() == location,
364+
selected: GoRouterState.of(context).uri.path == location,
365365
);
366366
}
367367

@@ -430,7 +430,7 @@ class IterableRouteWithDefaultValues extends GoRouteData {
430430
Widget drawerTile(BuildContext context) => ListTile(
431431
title: const Text('IterableRouteWithDefaultValues'),
432432
onTap: () => go(context),
433-
selected: GoRouterState.of(context).uri.toString() == location,
433+
selected: GoRouterState.of(context).uri.path == location,
434434
);
435435
}
436436

@@ -536,7 +536,9 @@ class BasePage<T> extends StatelessWidget {
536536
Text(
537537
'Query param with default value: $queryParamWithDefaultValue',
538538
),
539-
SelectableText(GoRouterState.of(context).uri.toString()),
539+
SelectableText(GoRouterState.of(context).uri.path),
540+
SelectableText(
541+
GoRouterState.of(context).uri.queryParameters.toString()),
540542
],
541543
),
542544
),

packages/go_router_builder/example/lib/shell_route_example.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class MyShellRouteScreen extends StatelessWidget {
7777
final Widget child;
7878

7979
int getCurrentIndex(BuildContext context) {
80-
final String location = GoRouterState.of(context).uri.toString();
80+
final String location = GoRouterState.of(context).uri.path;
8181
if (location == '/bar') {
8282
return 1;
8383
}

packages/go_router_builder/example/lib/shell_route_with_keys_example.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class MyShellRouteScreen extends StatelessWidget {
5757
final Widget child;
5858

5959
int getCurrentIndex(BuildContext context) {
60-
final String location = GoRouterState.of(context).uri.toString();
60+
final String location = GoRouterState.of(context).uri.path;
6161
if (location.startsWith('/users')) {
6262
return 1;
6363
}

packages/go_router_builder/example/lib/shell_route_with_observers_example.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class MyShellRouteScreen extends StatelessWidget {
6060
final Widget child;
6161

6262
int getCurrentIndex(BuildContext context) {
63-
final String location = GoRouterState.of(context).uri.toString();
63+
final String location = GoRouterState.of(context).uri.path;
6464
if (location.startsWith('/users')) {
6565
return 1;
6666
}

packages/go_router_builder/example/test/all_types_test.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,10 @@ void main() {
135135
).go(scaffoldState.context);
136136
await tester.pumpAndSettle();
137137
expect(find.text('IterableRoute'), findsOneWidget);
138+
expect(find.text('/iterable-route'), findsOneWidget);
138139
expect(
139140
find.text(
140-
'/iterable-route?enum-iterable-field=football&int-list-field=1&int-list-field=2&int-list-field=3&enum-only-in-set-field=burger&enum-only-in-set-field=pizza'),
141+
'{enum-iterable-field: football, int-list-field: 3, enum-only-in-set-field: pizza}'),
141142
findsOneWidget);
142143
});
143144

packages/go_router_builder/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: go_router_builder
22
description: >-
33
A builder that supports generated strongly-typed route helpers for
44
package:go_router
5-
version: 2.5.0
5+
version: 2.5.1
66
repository: https://github.com/flutter/packages/tree/main/packages/go_router_builder
77
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+go_router_builder%22
88

0 commit comments

Comments
 (0)