Skip to content

Commit f747441

Browse files
authored
[go_router]Show ShellRoute's routes in debug log (flutter#3664)
[go_router]Show ShellRoute's routes in debug log
1 parent fe5615f commit f747441

File tree

5 files changed

+86
-3
lines changed

5 files changed

+86
-3
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+
## 6.5.6
2+
3+
- Fixes an issue where ShellRoute routes were not logged when debugLogDiagnostic was enabled.
4+
15
## 6.5.5
26

37
- Fixes an issue when popping pageless route would accidentally complete imperative page.

packages/go_router/example/lib/shell_route.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class ShellRouteExampleApp extends StatelessWidget {
3131
final GoRouter _router = GoRouter(
3232
navigatorKey: _rootNavigatorKey,
3333
initialLocation: '/a',
34+
debugLogDiagnostics: true,
3435
routes: <RouteBase>[
3536
/// Application shell
3637
ShellRoute(

packages/go_router/lib/src/configuration.dart

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class RouteConfiguration {
2626
assert(_debugCheckParentNavigatorKeys(
2727
routes, <GlobalKey<NavigatorState>>[navigatorKey])) {
2828
_cacheNameToPath('', routes);
29-
log.info(_debugKnownRoutes());
29+
log.info(debugKnownRoutes());
3030
}
3131

3232
static bool _debugCheckPath(List<RouteBase> routes, bool isTopLevel) {
@@ -172,7 +172,12 @@ class RouteConfiguration {
172172
return 'RouterConfiguration: $routes';
173173
}
174174

175-
String _debugKnownRoutes() {
175+
/// Returns the full path of [routes].
176+
///
177+
/// Each path is indented based depth of the hierarchy, and its `name`
178+
/// is also appended if not null
179+
@visibleForTesting
180+
String debugKnownRoutes() {
176181
final StringBuffer sb = StringBuffer();
177182
sb.writeln('Full paths for routes:');
178183
_debugFullPathsFor(routes, '', 0, sb);
@@ -194,6 +199,8 @@ class RouteConfiguration {
194199
final String fullpath = concatenatePaths(parentFullpath, route.path);
195200
sb.writeln(' => ${''.padLeft(depth * 2)}$fullpath');
196201
_debugFullPathsFor(route.routes, fullpath, depth + 1, sb);
202+
} else if (route is ShellRoute) {
203+
_debugFullPathsFor(route.routes, parentFullpath, depth, sb);
197204
}
198205
}
199206
}

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: 6.5.5
4+
version: 6.5.6
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/configuration_test.dart

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,77 @@ void main() {
502502
throwsAssertionError,
503503
);
504504
});
505+
506+
test(
507+
'All known route strings returned by debugKnownRoutes are correct',
508+
() {
509+
final GlobalKey<NavigatorState> root =
510+
GlobalKey<NavigatorState>(debugLabel: 'root');
511+
final GlobalKey<NavigatorState> shell =
512+
GlobalKey<NavigatorState>(debugLabel: 'shell');
513+
514+
expect(
515+
RouteConfiguration(
516+
navigatorKey: root,
517+
routes: <RouteBase>[
518+
GoRoute(
519+
path: '/a',
520+
parentNavigatorKey: root,
521+
builder: _mockScreenBuilder,
522+
routes: <RouteBase>[
523+
ShellRoute(
524+
navigatorKey: shell,
525+
builder: _mockShellBuilder,
526+
routes: <RouteBase>[
527+
GoRoute(
528+
path: 'b',
529+
parentNavigatorKey: shell,
530+
builder: _mockScreenBuilder,
531+
),
532+
GoRoute(
533+
path: 'c',
534+
parentNavigatorKey: shell,
535+
builder: _mockScreenBuilder,
536+
),
537+
],
538+
),
539+
],
540+
),
541+
GoRoute(
542+
path: '/d',
543+
parentNavigatorKey: root,
544+
builder: _mockScreenBuilder,
545+
routes: <RouteBase>[
546+
GoRoute(
547+
path: 'e',
548+
parentNavigatorKey: root,
549+
builder: _mockScreenBuilder,
550+
routes: <RouteBase>[
551+
GoRoute(
552+
path: 'f',
553+
parentNavigatorKey: root,
554+
builder: _mockScreenBuilder,
555+
),
556+
],
557+
),
558+
],
559+
),
560+
],
561+
redirectLimit: 10,
562+
topRedirect: (BuildContext context, GoRouterState state) {
563+
return null;
564+
},
565+
).debugKnownRoutes(),
566+
'Full paths for routes:\n'
567+
' => /a\n'
568+
' => /a/b\n'
569+
' => /a/c\n'
570+
' => /d\n'
571+
' => /d/e\n'
572+
' => /d/e/f\n',
573+
);
574+
},
575+
);
505576
});
506577
}
507578

0 commit comments

Comments
 (0)