invokeCallback method

  1. @override
Future<bool> invokeCallback(
  1. Future<bool> defaultValue
)

Handles a pop route request.

This method prioritizes the children list in reverse order and calls ChildBackButtonDispatcher.notifiedByParent on them. If any of them handles the request (by returning a future with true), it exits this method by returning this future. Otherwise, it keeps moving on to the next child until a child handles the request. If none of the children handles the request, this back button dispatcher will then try to handle the request by itself. This back button dispatcher handles the request by notifying the router which in turn calls the RouterDelegate.popRoute and returns its result.

To decide whether this back button dispatcher will handle the pop route request, you can override the RouterDelegate.popRoute of the router delegate you pass into the router with this back button dispatcher to return a future of true or false.

Implementation

@override
Future<bool> invokeCallback(Future<bool> defaultValue) {
  if (_children.isNotEmpty) {
    final List<ChildBackButtonDispatcher> children = _children.toList();
    int childIndex = children.length - 1;

    Future<bool> notifyNextChild(bool result) {
      // If the previous child handles the callback, we return the result.
      if (result) {
        return SynchronousFuture<bool>(result);
      }
      // If the previous child did not handle the callback, we ask the next
      // child to handle the it.
      if (childIndex > 0) {
        childIndex -= 1;
        return children[childIndex]
          .notifiedByParent(defaultValue)
          .then<bool>(notifyNextChild);
      }
      // If none of the child handles the callback, the parent will then handle it.
      return super.invokeCallback(defaultValue);
    }

    return children[childIndex]
      .notifiedByParent(defaultValue)
      .then<bool>(notifyNextChild);
  }
  return super.invokeCallback(defaultValue);
}