From bb9a304aee8f9589a2be12f2a6034ef55056ea1b Mon Sep 17 00:00:00 2001 From: Chun-Heng Tai Date: Thu, 26 Oct 2023 16:53:24 -0700 Subject: [PATCH 1/2] [go_router] Fixes crashes when dynamically updates routing tables with named routes. --- packages/go_router/CHANGELOG.md | 4 ++ .../go_router/example/lib/routing_config.dart | 2 +- packages/go_router/lib/src/configuration.dart | 1 + packages/go_router/pubspec.yaml | 2 +- .../go_router/test/routing_config_test.dart | 39 +++++++++++++++++++ 5 files changed, 46 insertions(+), 2 deletions(-) diff --git a/packages/go_router/CHANGELOG.md b/packages/go_router/CHANGELOG.md index 78fee03acac..6791a2c917e 100644 --- a/packages/go_router/CHANGELOG.md +++ b/packages/go_router/CHANGELOG.md @@ -1,3 +1,7 @@ +## 12.0.3 + +- Fixes crashes when dynamically updates routing tables with named routes. + ## 12.0.2 - Fixes the problem that pathParameters is null in redirect when the Router is recreated. diff --git a/packages/go_router/example/lib/routing_config.dart b/packages/go_router/example/lib/routing_config.dart index 34255208d1e..85acf20c918 100644 --- a/packages/go_router/example/lib/routing_config.dart +++ b/packages/go_router/example/lib/routing_config.dart @@ -48,7 +48,7 @@ class _MyAppState extends State { return Scaffold( appBar: AppBar(title: const Text('Home')), body: Center( - child: Row( + child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ ElevatedButton( diff --git a/packages/go_router/lib/src/configuration.dart b/packages/go_router/lib/src/configuration.dart index 16f38f3b271..a5ff0fda5b4 100644 --- a/packages/go_router/lib/src/configuration.dart +++ b/packages/go_router/lib/src/configuration.dart @@ -196,6 +196,7 @@ class RouteConfiguration { assert(_debugCheckParentNavigatorKeys( routingTable.routes, >[navigatorKey])); assert(_debugCheckStatefulShellBranchDefaultLocations(routingTable.routes)); + _nameToPath.clear(); _cacheNameToPath('', routingTable.routes); log(debugKnownRoutes()); } diff --git a/packages/go_router/pubspec.yaml b/packages/go_router/pubspec.yaml index dfce6f6feed..e2c5cb5aa0b 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: 12.0.2 +version: 12.0.3 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/routing_config_test.dart b/packages/go_router/test/routing_config_test.dart index 87a866dd646..df2dc62e61b 100644 --- a/packages/go_router/test/routing_config_test.dart +++ b/packages/go_router/test/routing_config_test.dart @@ -106,4 +106,43 @@ void main() { await tester.pumpAndSettle(); expect(find.text('error'), findsOneWidget); }); + + testWidgets('routing config works with named route', + (WidgetTester tester) async { + final ValueNotifier config = ValueNotifier( + RoutingConfig( + routes: [ + GoRoute(path: '/', builder: (_, __) => const Text('home')), + GoRoute(path: '/abc', name: 'abc', builder: (_, __) => const Text('/abc')), + ], + ), + ); + final GoRouter router = await createRouterWithRoutingConfig( + config, + tester, + errorBuilder: (_, __) => const Text('error'), + ); + expect(find.text('home'), findsOneWidget); + // Sanity check. + router.goNamed('abc'); + await tester.pumpAndSettle(); + expect(find.text('/abc'), findsOneWidget); + + config.value = RoutingConfig( + routes: [ + GoRoute(path: '/', name: 'home', builder: (_, __) => const Text('home')), + GoRoute(path: '/abc', name: 'def',builder: (_, __) => const Text('def')), + ], + ); + await tester.pumpAndSettle(); + expect(find.text('def'), findsOneWidget); + + router.goNamed('home'); + await tester.pumpAndSettle(); + expect(find.text('home'), findsOneWidget); + + router.goNamed('def'); + await tester.pumpAndSettle(); + expect(find.text('def'), findsOneWidget); + }); } From 07203fecde2bd7193876eeaf25c461cc951964d7 Mon Sep 17 00:00:00 2001 From: Chun-Heng Tai Date: Thu, 2 Nov 2023 15:32:52 -0700 Subject: [PATCH 2/2] format --- .../go_router/test/routing_config_test.dart | 73 ++++++++++--------- 1 file changed, 39 insertions(+), 34 deletions(-) diff --git a/packages/go_router/test/routing_config_test.dart b/packages/go_router/test/routing_config_test.dart index df2dc62e61b..da36885e003 100644 --- a/packages/go_router/test/routing_config_test.dart +++ b/packages/go_router/test/routing_config_test.dart @@ -108,41 +108,46 @@ void main() { }); testWidgets('routing config works with named route', - (WidgetTester tester) async { - final ValueNotifier config = ValueNotifier( - RoutingConfig( - routes: [ - GoRoute(path: '/', builder: (_, __) => const Text('home')), - GoRoute(path: '/abc', name: 'abc', builder: (_, __) => const Text('/abc')), - ], - ), - ); - final GoRouter router = await createRouterWithRoutingConfig( - config, - tester, - errorBuilder: (_, __) => const Text('error'), - ); - expect(find.text('home'), findsOneWidget); - // Sanity check. - router.goNamed('abc'); - await tester.pumpAndSettle(); - expect(find.text('/abc'), findsOneWidget); + (WidgetTester tester) async { + final ValueNotifier config = ValueNotifier( + RoutingConfig( + routes: [ + GoRoute(path: '/', builder: (_, __) => const Text('home')), + GoRoute( + path: '/abc', + name: 'abc', + builder: (_, __) => const Text('/abc')), + ], + ), + ); + final GoRouter router = await createRouterWithRoutingConfig( + config, + tester, + errorBuilder: (_, __) => const Text('error'), + ); + expect(find.text('home'), findsOneWidget); + // Sanity check. + router.goNamed('abc'); + await tester.pumpAndSettle(); + expect(find.text('/abc'), findsOneWidget); - config.value = RoutingConfig( - routes: [ - GoRoute(path: '/', name: 'home', builder: (_, __) => const Text('home')), - GoRoute(path: '/abc', name: 'def',builder: (_, __) => const Text('def')), - ], - ); - await tester.pumpAndSettle(); - expect(find.text('def'), findsOneWidget); + config.value = RoutingConfig( + routes: [ + GoRoute( + path: '/', name: 'home', builder: (_, __) => const Text('home')), + GoRoute( + path: '/abc', name: 'def', builder: (_, __) => const Text('def')), + ], + ); + await tester.pumpAndSettle(); + expect(find.text('def'), findsOneWidget); - router.goNamed('home'); - await tester.pumpAndSettle(); - expect(find.text('home'), findsOneWidget); + router.goNamed('home'); + await tester.pumpAndSettle(); + expect(find.text('home'), findsOneWidget); - router.goNamed('def'); - await tester.pumpAndSettle(); - expect(find.text('def'), findsOneWidget); - }); + router.goNamed('def'); + await tester.pumpAndSettle(); + expect(find.text('def'), findsOneWidget); + }); }