diff --git a/packages/go_router/CHANGELOG.md b/packages/go_router/CHANGELOG.md index 774d71c756e..576a71789a7 100644 --- a/packages/go_router/CHANGELOG.md +++ b/packages/go_router/CHANGELOG.md @@ -1,3 +1,7 @@ +## 13.2.4 + +- Updates examples to use uri.path instead of uri.toString() for accessing the current location. + ## 13.2.3 - Fixes an issue where deep links without path caused an exception diff --git a/packages/go_router/example/lib/shell_route.dart b/packages/go_router/example/lib/shell_route.dart index 2d5d8765709..1628151d937 100644 --- a/packages/go_router/example/lib/shell_route.dart +++ b/packages/go_router/example/lib/shell_route.dart @@ -152,7 +152,7 @@ class ScaffoldWithNavBar extends StatelessWidget { } static int _calculateSelectedIndex(BuildContext context) { - final String location = GoRouterState.of(context).uri.toString(); + final String location = GoRouterState.of(context).uri.path; if (location.startsWith('/a')) { return 0; } diff --git a/packages/go_router/example/lib/shell_route_top_route.dart b/packages/go_router/example/lib/shell_route_top_route.dart index 35021297dfc..ce6128a559d 100644 --- a/packages/go_router/example/lib/shell_route_top_route.dart +++ b/packages/go_router/example/lib/shell_route_top_route.dart @@ -195,7 +195,7 @@ class ScaffoldWithNavBar extends StatelessWidget { } static int _calculateSelectedIndex(BuildContext context) { - final String location = GoRouterState.of(context).uri.toString(); + final String location = GoRouterState.of(context).uri.path; if (location.startsWith('/a')) { return 0; } diff --git a/packages/go_router/pubspec.yaml b/packages/go_router/pubspec.yaml index 1fc99858822..29dfaa27a44 100644 --- a/packages/go_router/pubspec.yaml +++ b/packages/go_router/pubspec.yaml @@ -1,7 +1,7 @@ name: go_router description: A declarative router for Flutter based on Navigation 2 supporting deep linking, data-driven routes and more -version: 13.2.3 +version: 13.2.4 repository: https://github.com/flutter/packages/tree/main/packages/go_router issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+go_router%22 diff --git a/packages/go_router/test/go_router_state_test.dart b/packages/go_router/test/go_router_state_test.dart index 7a1b73dd93e..69364f2aa80 100644 --- a/packages/go_router/test/go_router_state_test.dart +++ b/packages/go_router/test/go_router_state_test.dart @@ -42,7 +42,7 @@ void main() { path: '/', builder: (_, __) { return Builder(builder: (BuildContext context) { - return Text('1 ${GoRouterState.of(context).uri}'); + return Text('1 ${GoRouterState.of(context).uri.path}'); }); }, routes: [ @@ -50,15 +50,15 @@ void main() { path: 'a', builder: (_, __) { return Builder(builder: (BuildContext context) { - return Text('2 ${GoRouterState.of(context).uri}'); + return Text('2 ${GoRouterState.of(context).uri.path}'); }); }), ]), ]; final GoRouter router = await createRouter(routes, tester); - router.go('/?p=123'); + router.go('/'); await tester.pumpAndSettle(); - expect(find.text('1 /?p=123'), findsOneWidget); + expect(find.text('1 /'), findsOneWidget); router.go('/a'); await tester.pumpAndSettle(); @@ -74,7 +74,7 @@ void main() { path: '/', builder: (_, __) { return Builder(builder: (BuildContext context) { - return Text('1 ${GoRouterState.of(context).uri}'); + return Text('1 ${GoRouterState.of(context).uri.path}'); }); }, routes: [ @@ -110,7 +110,7 @@ void main() { path: '/', builder: (_, __) { return Builder(builder: (BuildContext context) { - return Text(GoRouterState.of(context).uri.toString()); + return Text(GoRouterState.of(context).uri.path); }); }, routes: [ @@ -118,15 +118,14 @@ void main() { path: 'a', builder: (_, __) { return Builder(builder: (BuildContext context) { - return Text( - key: key, GoRouterState.of(context).uri.toString()); + return Text(key: key, GoRouterState.of(context).uri.path); }); }), ]), ]; final GoRouter router = - await createRouter(routes, tester, initialLocation: '/a?p=123'); - expect(tester.widget(find.byKey(key)).data, '/a?p=123'); + await createRouter(routes, tester, initialLocation: '/a'); + expect(tester.widget(find.byKey(key)).data, '/a'); final GoRouterStateRegistry registry = tester .widget( find.byType(GoRouterStateRegistryScope)) @@ -136,7 +135,7 @@ void main() { await tester.pump(); expect(registry.registry.length, 2); // should retain the same location even if the location has changed. - expect(tester.widget(find.byKey(key)).data, '/a?p=123'); + expect(tester.widget(find.byKey(key)).data, '/a'); // Finish the pop animation. await tester.pumpAndSettle(); @@ -153,7 +152,7 @@ void main() { path: '/', builder: (_, __) { return Builder(builder: (BuildContext context) { - return Text(GoRouterState.of(context).uri.toString()); + return Text(GoRouterState.of(context).uri.path); }); }, routes: [ @@ -161,15 +160,14 @@ void main() { path: 'a', builder: (_, __) { return Builder(builder: (BuildContext context) { - return Text( - key: key, GoRouterState.of(context).uri.toString()); + return Text(key: key, GoRouterState.of(context).uri.path); }); }), ]), ]; await createRouter(routes, tester, - initialLocation: '/a?p=123', navigatorKey: nav); - expect(tester.widget(find.byKey(key)).data, '/a?p=123'); + initialLocation: '/a', navigatorKey: nav); + expect(tester.widget(find.byKey(key)).data, '/a'); final GoRouterStateRegistry registry = tester .widget( find.byType(GoRouterStateRegistryScope)) @@ -179,7 +177,7 @@ void main() { await tester.pump(); expect(registry.registry.length, 2); // should retain the same location even if the location has changed. - expect(tester.widget(find.byKey(key)).data, '/a?p=123'); + expect(tester.widget(find.byKey(key)).data, '/a'); // Finish the pop animation. await tester.pumpAndSettle(); diff --git a/packages/go_router_builder/CHANGELOG.md b/packages/go_router_builder/CHANGELOG.md index 8c7bf3ee834..096e927331c 100644 --- a/packages/go_router_builder/CHANGELOG.md +++ b/packages/go_router_builder/CHANGELOG.md @@ -1,3 +1,7 @@ +## 2.5.1 + +- Updates examples to use uri.path instead of uri.toString() for accessing the current location. + ## 2.5.0 * Updates minimum supported SDK version to Flutter 3.13/Dart 3.1. diff --git a/packages/go_router_builder/example/lib/all_types.dart b/packages/go_router_builder/example/lib/all_types.dart index aa49e45c83e..63d73caa4c0 100644 --- a/packages/go_router_builder/example/lib/all_types.dart +++ b/packages/go_router_builder/example/lib/all_types.dart @@ -58,7 +58,7 @@ class BigIntRoute extends GoRouteData { Widget drawerTile(BuildContext context) => ListTile( title: const Text('BigIntRoute'), onTap: () => go(context), - selected: GoRouterState.of(context).uri.toString() == location, + selected: GoRouterState.of(context).uri.path == location, ); } @@ -84,7 +84,7 @@ class BoolRoute extends GoRouteData { Widget drawerTile(BuildContext context) => ListTile( title: const Text('BoolRoute'), onTap: () => go(context), - selected: GoRouterState.of(context).uri.toString() == location, + selected: GoRouterState.of(context).uri.path == location, ); } @@ -107,7 +107,7 @@ class DateTimeRoute extends GoRouteData { Widget drawerTile(BuildContext context) => ListTile( title: const Text('DateTimeRoute'), onTap: () => go(context), - selected: GoRouterState.of(context).uri.toString() == location, + selected: GoRouterState.of(context).uri.path == location, ); } @@ -133,7 +133,7 @@ class DoubleRoute extends GoRouteData { Widget drawerTile(BuildContext context) => ListTile( title: const Text('DoubleRoute'), onTap: () => go(context), - selected: GoRouterState.of(context).uri.toString() == location, + selected: GoRouterState.of(context).uri.path == location, ); } @@ -159,7 +159,7 @@ class IntRoute extends GoRouteData { Widget drawerTile(BuildContext context) => ListTile( title: const Text('IntRoute'), onTap: () => go(context), - selected: GoRouterState.of(context).uri.toString() == location, + selected: GoRouterState.of(context).uri.path == location, ); } @@ -185,7 +185,7 @@ class NumRoute extends GoRouteData { Widget drawerTile(BuildContext context) => ListTile( title: const Text('NumRoute'), onTap: () => go(context), - selected: GoRouterState.of(context).uri.toString() == location, + selected: GoRouterState.of(context).uri.path == location, ); } @@ -212,7 +212,7 @@ class EnumRoute extends GoRouteData { Widget drawerTile(BuildContext context) => ListTile( title: const Text('EnumRoute'), onTap: () => go(context), - selected: GoRouterState.of(context).uri.toString() == location, + selected: GoRouterState.of(context).uri.path == location, ); } @@ -239,7 +239,7 @@ class EnhancedEnumRoute extends GoRouteData { Widget drawerTile(BuildContext context) => ListTile( title: const Text('EnhancedEnumRoute'), onTap: () => go(context), - selected: GoRouterState.of(context).uri.toString() == location, + selected: GoRouterState.of(context).uri.path == location, ); } @@ -265,7 +265,7 @@ class StringRoute extends GoRouteData { Widget drawerTile(BuildContext context) => ListTile( title: const Text('StringRoute'), onTap: () => go(context), - selected: GoRouterState.of(context).uri.toString() == location, + selected: GoRouterState.of(context).uri.path == location, ); } @@ -288,7 +288,7 @@ class UriRoute extends GoRouteData { Widget drawerTile(BuildContext context) => ListTile( title: const Text('UriRoute'), onTap: () => go(context), - selected: GoRouterState.of(context).uri.toString() == location, + selected: GoRouterState.of(context).uri.path == location, ); } @@ -361,7 +361,7 @@ class IterableRoute extends GoRouteData { Widget drawerTile(BuildContext context) => ListTile( title: const Text('IterableRoute'), onTap: () => go(context), - selected: GoRouterState.of(context).uri.toString() == location, + selected: GoRouterState.of(context).uri.path == location, ); } @@ -430,7 +430,7 @@ class IterableRouteWithDefaultValues extends GoRouteData { Widget drawerTile(BuildContext context) => ListTile( title: const Text('IterableRouteWithDefaultValues'), onTap: () => go(context), - selected: GoRouterState.of(context).uri.toString() == location, + selected: GoRouterState.of(context).uri.path == location, ); } @@ -536,7 +536,9 @@ class BasePage extends StatelessWidget { Text( 'Query param with default value: $queryParamWithDefaultValue', ), - SelectableText(GoRouterState.of(context).uri.toString()), + SelectableText(GoRouterState.of(context).uri.path), + SelectableText( + GoRouterState.of(context).uri.queryParameters.toString()), ], ), ), diff --git a/packages/go_router_builder/example/lib/shell_route_example.dart b/packages/go_router_builder/example/lib/shell_route_example.dart index 6b5ec27947d..93e1df207a8 100644 --- a/packages/go_router_builder/example/lib/shell_route_example.dart +++ b/packages/go_router_builder/example/lib/shell_route_example.dart @@ -77,7 +77,7 @@ class MyShellRouteScreen extends StatelessWidget { final Widget child; int getCurrentIndex(BuildContext context) { - final String location = GoRouterState.of(context).uri.toString(); + final String location = GoRouterState.of(context).uri.path; if (location == '/bar') { return 1; } diff --git a/packages/go_router_builder/example/lib/shell_route_with_keys_example.dart b/packages/go_router_builder/example/lib/shell_route_with_keys_example.dart index af79f41fb98..5448f7e1918 100644 --- a/packages/go_router_builder/example/lib/shell_route_with_keys_example.dart +++ b/packages/go_router_builder/example/lib/shell_route_with_keys_example.dart @@ -57,7 +57,7 @@ class MyShellRouteScreen extends StatelessWidget { final Widget child; int getCurrentIndex(BuildContext context) { - final String location = GoRouterState.of(context).uri.toString(); + final String location = GoRouterState.of(context).uri.path; if (location.startsWith('/users')) { return 1; } diff --git a/packages/go_router_builder/example/lib/shell_route_with_observers_example.dart b/packages/go_router_builder/example/lib/shell_route_with_observers_example.dart index a762e12c124..a69e05e749e 100644 --- a/packages/go_router_builder/example/lib/shell_route_with_observers_example.dart +++ b/packages/go_router_builder/example/lib/shell_route_with_observers_example.dart @@ -60,7 +60,7 @@ class MyShellRouteScreen extends StatelessWidget { final Widget child; int getCurrentIndex(BuildContext context) { - final String location = GoRouterState.of(context).uri.toString(); + final String location = GoRouterState.of(context).uri.path; if (location.startsWith('/users')) { return 1; } diff --git a/packages/go_router_builder/example/test/all_types_test.dart b/packages/go_router_builder/example/test/all_types_test.dart index 99a3824cf4e..eafcdeefda0 100644 --- a/packages/go_router_builder/example/test/all_types_test.dart +++ b/packages/go_router_builder/example/test/all_types_test.dart @@ -135,9 +135,10 @@ void main() { ).go(scaffoldState.context); await tester.pumpAndSettle(); expect(find.text('IterableRoute'), findsOneWidget); + expect(find.text('/iterable-route'), findsOneWidget); expect( find.text( - '/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'), + '{enum-iterable-field: football, int-list-field: 3, enum-only-in-set-field: pizza}'), findsOneWidget); }); diff --git a/packages/go_router_builder/pubspec.yaml b/packages/go_router_builder/pubspec.yaml index a3ffb517965..b552204767a 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: 2.5.0 +version: 2.5.1 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