@@ -98,7 +98,7 @@ class GoRouterState {
98
98
///
99
99
/// This method cannot be called during [GoRoute.pageBuilder] or
100
100
/// [ShellRoute.pageBuilder] since there is no [GoRouterState] to be
101
- /// associated with.
101
+ /// associated with yet .
102
102
///
103
103
/// To access GoRouterState from a widget.
104
104
///
@@ -116,26 +116,41 @@ class GoRouterState {
116
116
/// }
117
117
/// ```
118
118
static GoRouterState of (BuildContext context) {
119
- final ModalRoute <Object ?>? route = ModalRoute .of (context);
120
- if (route == null ) {
121
- throw GoError ('There is no modal route above the current context.' );
122
- }
123
- final RouteSettings settings = route.settings;
124
- if (settings is ! Page <Object ?>) {
125
- throw GoError (
126
- 'The parent route must be a page route to have a GoRouterState' );
127
- }
128
- final GoRouterStateRegistryScope ? scope = context
129
- .dependOnInheritedWidgetOfExactType <GoRouterStateRegistryScope >();
130
- if (scope == null ) {
131
- throw GoError (
132
- 'There is no GoRouterStateRegistryScope above the current context.' );
119
+ ModalRoute <Object ?>? route;
120
+ GoRouterStateRegistryScope ? scope;
121
+ while (true ) {
122
+ route = ModalRoute .of (context);
123
+ if (route == null ) {
124
+ throw _noGoRouterStateError;
125
+ }
126
+ final RouteSettings settings = route.settings;
127
+ if (settings is Page <Object ?>) {
128
+ scope = context
129
+ .dependOnInheritedWidgetOfExactType <GoRouterStateRegistryScope >();
130
+ if (scope == null ) {
131
+ throw _noGoRouterStateError;
132
+ }
133
+ final GoRouterState ? state = scope.notifier!
134
+ ._createPageRouteAssociation (
135
+ route.settings as Page <Object ?>, route);
136
+ if (state != null ) {
137
+ return state;
138
+ }
139
+ }
140
+ final NavigatorState ? state = Navigator .maybeOf (context);
141
+ if (state == null ) {
142
+ throw _noGoRouterStateError;
143
+ }
144
+ context = state.context;
133
145
}
134
- final GoRouterState state =
135
- scope.notifier! ._createPageRouteAssociation (settings, route);
136
- return state;
137
146
}
138
147
148
+ static GoError get _noGoRouterStateError => GoError (
149
+ 'There is no GoRouterState above the current context. '
150
+ 'This method should only be called under the sub tree of a '
151
+ 'RouteBase.builder.' ,
152
+ );
153
+
139
154
/// Get a location from route name and parameters.
140
155
/// This is useful for redirecting to a named location.
141
156
String namedLocation (
@@ -207,10 +222,12 @@ class GoRouterStateRegistry extends ChangeNotifier {
207
222
final Map <Route <Object ?>, Page <Object ?>> _routePageAssociation =
208
223
< ModalRoute <Object ?>, Page <Object ?>> {};
209
224
210
- GoRouterState _createPageRouteAssociation (
225
+ GoRouterState ? _createPageRouteAssociation (
211
226
Page <Object ?> page, ModalRoute <Object ?> route) {
212
227
assert (route.settings == page);
213
- assert (registry.containsKey (page));
228
+ if (! registry.containsKey (page)) {
229
+ return null ;
230
+ }
214
231
final Page <Object ?>? oldPage = _routePageAssociation[route];
215
232
if (oldPage == null ) {
216
233
// This is a new association.
0 commit comments