pop<T extends Object> method

  1. @optionalTypeArgs
bool pop <T extends Object>([T result ])
@optionalTypeArgs

Pop the top-most route off the navigator.

The current route's Route.didPop method is called first. If that method returns false, then this method returns true but nothing else is changed (the route is expected to have popped some internal state; see e.g. LocalHistoryRoute). Otherwise, the rest of this description applies.

If non-null, result will be used as the result of the route that is popped; the future that had been returned from pushing the popped route will complete with result. Routes such as dialogs or popup menus typically use this mechanism to return the value selected by the user to the widget that created their route. The type of result, if provided, must match the type argument of the class of the popped route (T).

The popped route and the route below it are notified (see Route.didPop, Route.didComplete, and Route.didPopNext). If the Navigator has any Navigator.observers, they will be notified as well (see NavigatorObserver.didPop).

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

Returns true if a route was popped (including if Route.didPop returned false); returns false if there are no further previous routes.

Typical usage for closing a route is as follows:
void _handleClose() {
  navigator.pop();
}

A dialog box might be closed with a result:
void _handleAccept() {
  navigator.pop(true); // dialog returns true
}

Implementation

@optionalTypeArgs
bool pop<T extends Object>([ T result ]) {
  assert(!_debugLocked);
  assert(() { _debugLocked = true; return true; }());
  final Route<dynamic> route = _history.last;
  assert(route._navigator == this);
  bool debugPredictedWouldPop;
  assert(() { debugPredictedWouldPop = !route.willHandlePopInternally; return true; }());
  if (route.didPop(result ?? route.currentResult)) {
    assert(debugPredictedWouldPop);
    if (_history.length > 1) {
      _history.removeLast();
      // If route._navigator is null, the route called finalizeRoute from
      // didPop, which means the route has already been disposed and doesn't
      // need to be added to _poppedRoutes for later disposal.
      if (route._navigator != null)
        _poppedRoutes.add(route);
      _history.last.didPopNext(route);
      for (NavigatorObserver observer in widget.observers)
        observer.didPop(route, _history.last);
      RouteNotificationMessages.maybeNotifyRouteChange(_routePoppedMethod, route, _history.last);
    } else {
      assert(() { _debugLocked = false; return true; }());
      return false;
    }
  } else {
    assert(!debugPredictedWouldPop);
  }
  assert(() { _debugLocked = false; return true; }());
  _afterNavigation<dynamic>(route);
  return true;
}