popRoute static method

Future popRoute()

All functions are called, in order. If any function returns true, the combined result is true, and the default button process will NOT be fired.

Only if all functions return false (or null), the combined result is false, and the default button process will be fired.

Each function gets a boolean that indicates the current combined result from the previous functions.

Note: If the interceptor throws an error, a message will be printed to the console, and a placeholder error will not be thrown. You can change the treatment of errors by changing the static errorProcessing field.

Implementation

static Future popRoute() async {
  bool stopDefaultButtonEvent = false;

  results.clear();

  List<_FunctionWithZIndex> interceptors = List.of(_interceptors);

  for (var i = 0; i < interceptors.length; i++) {
    bool? result;

    try {
      var interceptor = interceptors[i];

      if (!interceptor.ifNotYetIntercepted || !stopDefaultButtonEvent) {
        FutureOr<bool> _result = interceptor.interceptionFunction(
          stopDefaultButtonEvent,
          RouteInfo(routeWhenAdded: interceptor.routeWhenAdded),
        );

        if (_result is bool)
          result = _result;
        else if (_result is Future<bool>)
          result = await _result;
        else
          throw AssertionError(_result.runtimeType);

        results.results.add(InterceptorResult(interceptor.name, result));
      }
    } catch (error) {
      errorProcessing(error);
    }

    if (result == true) stopDefaultButtonEvent = true;
  }

  if (stopDefaultButtonEvent)
    return Future<dynamic>.value();
  else {
    results.ifDefaultButtonEventWasFired = true;
    return handlePopRouteFunction();
  }
}