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); + }); }