Skip to content

Commit 4dc074a

Browse files
committed
fix: PopScope.onPopInvokedWithResult not called in branch routes
1 parent 2e166de commit 4dc074a

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

packages/go_router/lib/src/delegate.dart

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ class GoRouterDelegate extends RouterDelegate<RouteMatchList>
5555

5656
@override
5757
Future<bool> popRoute() async {
58-
final NavigatorState? state = _findCurrentNavigator();
59-
if (state != null) {
58+
final List<NavigatorState> states = _findCurrentNavigators();
59+
for (final NavigatorState state in states) {
6060
final bool didPop = await state.maybePop(); // Call maybePop() directly
6161
if (didPop) {
6262
return true; // Return true if maybePop handled the pop
@@ -96,17 +96,25 @@ class GoRouterDelegate extends RouterDelegate<RouteMatchList>
9696

9797
/// Pops the top-most route.
9898
void pop<T extends Object?>([T? result]) {
99-
final NavigatorState? state = _findCurrentNavigator();
100-
if (state == null || !state.canPop()) {
99+
final Iterable<NavigatorState> states = _findCurrentNavigators().where(
100+
(NavigatorState element) => element.canPop(),
101+
);
102+
if (states.isEmpty) {
101103
throw GoError('There is nothing to pop');
102104
}
103-
state.pop(result);
105+
states.first.pop(result);
104106
}
105107

106-
NavigatorState? _findCurrentNavigator() {
107-
NavigatorState? state;
108-
state =
109-
navigatorKey.currentState; // Set state directly without canPop check
108+
/// Get a prioritized list of NavigatorStates
109+
/// 1. Pop routes within branches of shell navigation which `canPop`
110+
/// 2. Pop parent route
111+
/// 3. Pop branch routes (which are exit routes as they cannot be popped)
112+
List<NavigatorState> _findCurrentNavigators() {
113+
final List<NavigatorState> states = <NavigatorState>[];
114+
if (navigatorKey.currentState != null) {
115+
states.add(navigatorKey
116+
.currentState!); // Set state directly without canPop check
117+
}
110118

111119
RouteMatchBase walker = currentConfiguration.matches.last;
112120
while (walker is ShellRouteMatch) {
@@ -119,13 +127,11 @@ class GoRouterDelegate extends RouterDelegate<RouteMatchList>
119127
// Stop if there is a pageless route on top of the shell route.
120128
break;
121129
}
122-
123-
if (potentialCandidate.canPop()) {
124-
state = walker.navigatorKey.currentState;
125-
}
130+
states.insert(
131+
potentialCandidate.canPop() ? 0 : states.length, potentialCandidate);
126132
walker = walker.matches.last;
127133
}
128-
return state;
134+
return states;
129135
}
130136

131137
bool _handlePopPageWithRouteMatch(

0 commit comments

Comments
 (0)