Skip to content

Commit a50d124

Browse files
♻️ Move the asserts to the router deleguate
1 parent 934f28e commit a50d124

File tree

2 files changed

+12
-20
lines changed

2 files changed

+12
-20
lines changed

packages/go_router/lib/src/delegate.dart

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,18 @@ class GoRouterDelegate extends RouterDelegate<RouteMatchList>
148148
return navigatorKey.currentState?.canPop() ?? false;
149149
}
150150

151+
void _debugAssertMatchListNotEmpty() {
152+
assert(
153+
_matchList.isNotEmpty,
154+
'You have popped the last page off of the stack,'
155+
' there are no pages left to show',
156+
);
157+
}
158+
151159
/// Pop the top page off the GoRouter's page stack.
152160
void pop() {
153161
_matchList.pop();
162+
_debugAssertMatchListNotEmpty();
154163
notifyListeners();
155164
}
156165

@@ -159,10 +168,8 @@ class GoRouterDelegate extends RouterDelegate<RouteMatchList>
159168
/// See also:
160169
/// * [push] which pushes the given location onto the page stack.
161170
void replace(RouteMatch match) {
162-
// We pop the current page. There is no need to verify the match list is not
163-
// empty during the pop because we are going to push a new page right away.
164-
_matchList.pop(asserts: false);
165-
push(match); // [push] will notify listeners.
171+
_matchList.pop();
172+
push(match); // [push] will notify the listeners.
166173
}
167174

168175
/// For internal use; visible for testing only.

packages/go_router/lib/src/matching.dart

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,13 @@ class RouteMatchList {
7171
}
7272

7373
/// Removes the last match.
74-
void pop({bool asserts = true}) {
74+
void pop() {
7575
_matches.removeLast();
7676

77-
if (asserts) {
78-
_debugAssertNotEmpty();
79-
}
80-
8177
// Also pop ShellRoutes when there are no subsequent route matches
8278
while (_matches.isNotEmpty && _matches.last.route is ShellRoute) {
8379
_matches.removeLast();
8480
}
85-
86-
if (asserts) {
87-
_debugAssertNotEmpty();
88-
}
8981
}
9082

9183
/// An optional object provided by the app during navigation.
@@ -102,13 +94,6 @@ class RouteMatchList {
10294

10395
/// Returns the error that this match intends to display.
10496
Exception? get error => matches.first.error;
105-
106-
void _debugAssertNotEmpty() {
107-
assert(
108-
_matches.isNotEmpty,
109-
'You have popped the last page off of the stack,'
110-
' there are no pages left to show');
111-
}
11297
}
11398

11499
/// An error that occurred during matching.

0 commit comments

Comments
 (0)