removeRoute method

void removeRoute (Route route)

Immediately remove route from the navigator, and Route.dispose it.

The removed route is removed without being completed, so this method does not take a return value argument. No animations are run as a result of this method call.

The routes below and above the removed route are notified (see Route.didChangeNext and Route.didChangePrevious). If the Navigator has any Navigator.observers, they will be notified as well (see NavigatorObserver.didRemove). The removed route is disposed without being notified. The future that had been returned from pushing that routes will not complete.

The given route must be in the history; this method will throw an exception if it is not.

Ongoing gestures within the current route are canceled.

Implementation

void removeRoute(Route<dynamic> route) {
  assert(route != null);
  assert(!_debugLocked);
  assert(() { _debugLocked = true; return true; }());
  assert(route._navigator == this);
  final int index = _history.indexOf(route);
  assert(index != -1);
  final Route<dynamic> previousRoute = index > 0 ? _history[index - 1] : null;
  final Route<dynamic> nextRoute = (index + 1 < _history.length) ? _history[index + 1] : null;
  _history.removeAt(index);
  previousRoute?.didChangeNext(nextRoute);
  nextRoute?.didChangePrevious(previousRoute);
  for (NavigatorObserver observer in widget.observers)
    observer.didRemove(route, previousRoute);
  route.dispose();
  assert(() { _debugLocked = false; return true; }());
  _afterNavigation<dynamic>(nextRoute);
}