maybePop<T extends Object?> method

  1. @optionalTypeArgs
Future<bool> maybePop<T extends Object?>(
  1. [T? result]
)

Consults the current route's Route.popDisposition method, and acts accordingly, potentially popping the route as a result; returns whether the pop request should be considered handled.

If the RoutePopDisposition is RoutePopDisposition.pop, then the pop method is called, and this method returns true, indicating that it handled the pop request.

If the RoutePopDisposition is RoutePopDisposition.doNotPop, then this method returns true, but does not do anything beyond that.

If the RoutePopDisposition is RoutePopDisposition.bubble, then this method returns false, and the caller is responsible for sending the request to the containing scope (e.g. by closing the application).

This method is typically called for a user-initiated pop. For example on Android it's called by the binding for the system's back button.

The T type argument is the type of the return value of the current route. (Typically this isn't known; consider specifying dynamic or Null.)

See also:

  • Form, which provides a Form.canPop boolean that enables the form to prevent any pops initiated by the app's back button.
  • ModalRoute, which provides a scopedOnPopCallback that can be used to define the route's willPop method.

Implementation

@optionalTypeArgs
Future<bool> maybePop<T extends Object?>([ T? result ]) async {
  final _RouteEntry? lastEntry = _lastRouteEntryWhereOrNull(_RouteEntry.isPresentPredicate);
  if (lastEntry == null) {
    return false;
  }
  assert(lastEntry.route._navigator == this);

  // TODO(justinmc): When the deprecated willPop method is removed, delete
  // this code and use only popDisposition, below.
  final RoutePopDisposition willPopDisposition = await lastEntry.route.willPop();
  if (!mounted) {
    // Forget about this pop, we were disposed in the meantime.
    return true;
  }
  if (willPopDisposition == RoutePopDisposition.doNotPop) {
    return true;
  }
  final _RouteEntry? newLastEntry = _lastRouteEntryWhereOrNull(_RouteEntry.isPresentPredicate);
  if (lastEntry != newLastEntry) {
    // Forget about this pop, something happened to our history in the meantime.
    return true;
  }

  switch (lastEntry.route.popDisposition) {
    case RoutePopDisposition.bubble:
      return false;
    case RoutePopDisposition.pop:
      pop(result);
      return true;
    case RoutePopDisposition.doNotPop:
      lastEntry.route.onPopInvoked(false);
      return true;
  }
}