Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[go_router] Secured empty matches in canPop #8557

Merged
merged 14 commits into from
Feb 26, 2025
4 changes: 4 additions & 0 deletions packages/go_router/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 14.8.1

- Secured canPop method for the lack of matches in routerDelegate's configuration.

## 14.8.0

- Adds `preload` parameter to `StatefulShellBranchData.$branch`.
Expand Down
3 changes: 3 additions & 0 deletions packages/go_router/lib/src/delegate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ class GoRouterDelegate extends RouterDelegate<RouteMatchList>
if (navigatorKey.currentState?.canPop() ?? false) {
return true;
}
if (currentConfiguration.matches.isEmpty) {
return false;
}
RouteMatchBase walker = currentConfiguration.matches.last;
while (walker is ShellRouteMatch) {
if (walker.navigatorKey.currentState?.canPop() ?? false) {
Expand Down
2 changes: 1 addition & 1 deletion packages/go_router/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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: 14.8.0
version: 14.8.1
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

Expand Down
14 changes: 14 additions & 0 deletions packages/go_router/test/delegate_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,20 @@ void main() {
expect(goRouter.routerDelegate.canPop(), true);
},
);
testWidgets(
'It should return false if there are no matches in the stack',
(WidgetTester tester) async {
final GoRouter goRouter = GoRouter(
initialLocation: '/',
routes: <GoRoute>[],
);
addTearDown(goRouter.dispose);
await tester.pumpWidget(MaterialApp.router(routerConfig: goRouter));
await tester.pumpAndSettle();
expect(goRouter.routerDelegate.currentConfiguration.matches.length, 0);
expect(goRouter.routerDelegate.canPop(), false);
},
);
});

group('pushReplacement', () {
Expand Down