closeUntil method Null safety
override
Close the current top most location and
every location after it until location
is visible.
resultMap
of String location path to Object as result for screen.
Implementation
@override
void closeUntil({required String location, Map<String, Object?>? resultMap}) {
if (_pages.isEmpty) {
throw const DBRouterDelegateCantClosePageException(
"there's no page in the stack to close",
);
}
if (_pages.length < 2) {
throw const DBRouterDelegateCantClosePageException(
"You can't remove the only page in the stack",
);
}
final int locationIndex = _pages.indexWhere((DBPage page) {
return page.destination.path == location;
});
if (locationIndex < 0) {
throw DBRouterDelegateCantClosePageException(
'$location not found in the list of available\nPages: '
'${_pages.reversed.map((DBPage page) => page.destination.path)}',
);
}
assert(
(locationIndex + 1) < _pages.length,
"$location is the top most, so it's make no sense to use closeUntil, "
'use close method to close a single location',
);
final List<DBPage> pagesToClose = _pages.sublist(locationIndex + 1);
_pages.removeRange(locationIndex + 1, _pages.length);
// Run throughout the list of pages, from the top most page first and on.
for (final DBPage page in pagesToClose.reversed) {
final Completer<Object?>? tracker = _popResultTracker.remove(
page.destination.path,
);
final Object? result = resultMap?[page.destination.path];
if (tracker != null && !tracker.isCompleted) {
tracker.complete(result);
}
}
notifyListeners();
}