From cccf5d24d7031d730058c94370c74ca3175876ab Mon Sep 17 00:00:00 2001 From: chunhtai <47866232+chunhtai@users.noreply.github.com> Date: Thu, 2 Nov 2023 15:51:05 -0700 Subject: [PATCH] =?UTF-8?q?[go=5Frouter]=20Fixes=20crashes=20when=20dynami?= =?UTF-8?q?cally=20updates=20routing=20tables=20wit=E2=80=A6=20(#5242)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit …h named routes. fixes https://github.com/flutter/flutter/issues/137133 --- 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 | 44 +++++++++++++++++++ 5 files changed, 51 insertions(+), 2 deletions(-) diff --git a/packages/go_router/CHANGELOG.md b/packages/go_router/CHANGELOG.md index 78fee03acac7..6791a2c917e7 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 34255208d1e0..85acf20c9181 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 16f38f3b2719..a5ff0fda5b43 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 dfce6f6feedb..e2c5cb5aa0b9 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 87a866dd6464..da36885e0037 100644 --- a/packages/go_router/test/routing_config_test.dart +++ b/packages/go_router/test/routing_config_test.dart @@ -106,4 +106,48 @@ 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); + }); }