@@ -55,8 +55,8 @@ class GoRouterDelegate extends RouterDelegate<RouteMatchList>
55
55
56
56
@override
57
57
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 ) {
60
60
final bool didPop = await state.maybePop (); // Call maybePop() directly
61
61
if (didPop) {
62
62
return true ; // Return true if maybePop handled the pop
@@ -96,17 +96,25 @@ class GoRouterDelegate extends RouterDelegate<RouteMatchList>
96
96
97
97
/// Pops the top-most route.
98
98
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) {
101
103
throw GoError ('There is nothing to pop' );
102
104
}
103
- state .pop (result);
105
+ states.first .pop (result);
104
106
}
105
107
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
+ }
110
118
111
119
RouteMatchBase walker = currentConfiguration.matches.last;
112
120
while (walker is ShellRouteMatch ) {
@@ -119,13 +127,11 @@ class GoRouterDelegate extends RouterDelegate<RouteMatchList>
119
127
// Stop if there is a pageless route on top of the shell route.
120
128
break ;
121
129
}
122
-
123
- if (potentialCandidate.canPop ()) {
124
- state = walker.navigatorKey.currentState;
125
- }
130
+ states.insert (
131
+ potentialCandidate.canPop () ? 0 : states.length, potentialCandidate);
126
132
walker = walker.matches.last;
127
133
}
128
- return state ;
134
+ return states ;
129
135
}
130
136
131
137
bool _handlePopPageWithRouteMatch (
0 commit comments