replace<T extends Object> method

  1. @optionalTypeArgs
void replace <T extends Object>({@required Route oldRoute, @required Route<T> newRoute })
@optionalTypeArgs

Replaces a route on the navigator with a new route.

The old route must not be current visible, as this method skips the animations and therefore the removal would be jarring if it was visible. To replace the top-most route, consider pushReplacement instead, which does animate the new route, and delays removing the old route until the new route has finished animating.

The removed route is removed without being completed, so this method does not take a return value argument.

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

This can be useful in combination with removeRouteBelow when building a non-linear user experience.

The T type argument is the type of the return value of the new route.

See also:

  • replaceRouteBelow, which is the same but identifies the route to be removed by reference to the route above it, rather than directly.

Implementation

@optionalTypeArgs
void replace<T extends Object>({ @required Route<dynamic> oldRoute, @required Route<T> newRoute }) {
  assert(!_debugLocked);
  assert(oldRoute != null);
  assert(newRoute != null);
  if (oldRoute == newRoute)
    return;
  assert(() { _debugLocked = true; return true; }());
  assert(oldRoute._navigator == this);
  assert(newRoute._navigator == null);
  assert(oldRoute.overlayEntries.isNotEmpty);
  assert(newRoute.overlayEntries.isEmpty);
  assert(!overlay.debugIsVisible(oldRoute.overlayEntries.last));
  final int index = _history.indexOf(oldRoute);
  assert(index >= 0);
  newRoute._navigator = this;
  newRoute.install(oldRoute.overlayEntries.last);
  _history[index] = newRoute;
  newRoute.didReplace(oldRoute);
  if (index + 1 < _history.length) {
    newRoute.didChangeNext(_history[index + 1]);
    _history[index + 1].didChangePrevious(newRoute);
  } else {
    newRoute.didChangeNext(null);
  }
  if (index > 0) {
    _history[index - 1].didChangeNext(newRoute);
    newRoute.didChangePrevious(_history[index - 1]);
  }
  for (NavigatorObserver observer in widget.observers)
    observer.didReplace(newRoute: newRoute, oldRoute: oldRoute);
  oldRoute.dispose();
  assert(() { _debugLocked = false; return true; }());
}