Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions packages/go_router/lib/src/delegate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ class GoRouterDelegate extends RouterDelegate<RouteMatchList> with ChangeNotifie
return true; // Return true if maybePop handled the pop
}
}

if (currentConfiguration.isEmpty) {
return false;
}
// Fallback to onExit if maybePop did not handle the pop
final GoRoute lastRoute = currentConfiguration.last.route;
if (lastRoute.onExit != null && navigatorKey.currentContext != null) {
Expand All @@ -79,7 +81,7 @@ class GoRouterDelegate extends RouterDelegate<RouteMatchList> with ChangeNotifie
if (navigatorKey.currentState?.canPop() ?? false) {
return true;
}
if (currentConfiguration.matches.isEmpty) {
if (currentConfiguration.isEmpty) {
return false;
}
RouteMatchBase walker = currentConfiguration.matches.last;
Expand Down Expand Up @@ -115,7 +117,9 @@ class GoRouterDelegate extends RouterDelegate<RouteMatchList> with ChangeNotifie
// Set state directly without canPop check
states.add(navigatorKey.currentState!);
}

if (currentConfiguration.matches.isEmpty) {
return states.reversed;
}
Comment thread
abhaysaqi marked this conversation as resolved.
Outdated
Comment thread
abhaysaqi marked this conversation as resolved.
Outdated
RouteMatchBase walker = currentConfiguration.matches.last;
while (walker is ShellRouteMatch) {
final NavigatorState potentialCandidate = walker.navigatorKey.currentState!;
Expand Down
17 changes: 17 additions & 0 deletions packages/go_router/test/delegate_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,23 @@ void main() {
});
});

testWidgets('popRoute does not throw Bad state when currentConfiguration is empty', (
WidgetTester tester,
) async {
final GoRouter goRouter = GoRouter(
initialLocation: '/',
routes: <GoRoute>[GoRoute(path: '/', builder: (_, _) => const DummyStatefulWidget())],
);
addTearDown(goRouter.dispose);
await tester.pumpWidget(MaterialApp.router(routerConfig: goRouter));

// Simulate empty configuration (e.g. startup window before first route resolves)
goRouter.routerDelegate.currentConfiguration = RouteMatchList.empty;

// Should return false without throwing "Bad state: No element"
expect(await goRouter.routerDelegate.popRoute(), isFalse);
});

group('push', () {
testWidgets('It should return different pageKey when push is called', (
WidgetTester tester,
Expand Down