pop<T extends Object?> method

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

Pop the top-most route off the navigator.

The current route's Route.didPop method is called first. If that method returns false, then the route remains in the Navigator's history (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.

The type of result, if provided, must match the type argument of the class of the popped route (T).

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

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

Implementation

@optionalTypeArgs
void pop<T extends Object?>([ T? result ]) {
  assert(!_debugLocked);
  assert(() {
    _debugLocked = true;
    return true;
  }());
  final _RouteEntry entry = _history.lastWhere(_RouteEntry.isPresentPredicate);
  if (entry.pageBased && widget.onPopPage != null) {
    if (widget.onPopPage!(entry.route, result) && entry.currentState.index <= _RouteLifecycle.idle.index) {
      // The entry may have been disposed if the pop finishes synchronously.
      assert(entry.route._popCompleter.isCompleted);
      entry.currentState = _RouteLifecycle.pop;
    }
    entry.route.onPopInvokedWithResult(true, result);
  } else {
    entry.pop<T>(result);
    assert (entry.currentState == _RouteLifecycle.pop);
  }
  if (entry.currentState == _RouteLifecycle.pop) {
    _flushHistoryUpdates(rearrangeOverlay: false);
  }
  assert(entry.currentState == _RouteLifecycle.idle || entry.route._popCompleter.isCompleted);
  assert(() {
    _debugLocked = false;
    return true;
  }());
  _afterNavigation(entry.route);
}