invoke<T extends Intent> method

Object invoke <T extends Intent>(
  1. BuildContext context,
  2. T intent,
  3. {bool nullOk: false}
)

Invokes the action associated with the given Intent using the Actions widget that most tightly encloses the given BuildContext.

The context, intent and nullOk arguments must not be null.

If the given intent isn't found in the first Actions.actions map, then it will look to the next Actions widget in the hierarchy until it reaches the root.

Will throw if no ambient Actions widget is found, or if the given intent doesn't map to an action in any of the Actions.actions maps that are found.

Setting nullOk to true means that if no ambient Actions widget is found, then this method will return null instead of throwing.

Returns the result of invoking the action's Action.invoke method. If no action mapping was found for the specified intent, or if the action that was found was disabled, then this returns null. Callers can detect whether or not the action is available (found, and not disabled) using Actions.find with its nullOk argument set to true.

Implementation

static Object invoke<T extends Intent>(
  BuildContext context,
  T intent, {
  bool nullOk = false,
}) {
  assert(intent != null);
  assert(nullOk != null);
  assert(context != null);
  Action<T> action;
  InheritedElement actionElement;

  _visitActionsAncestors(context, (InheritedElement element) {
    final _ActionsMarker actions = element.widget as _ActionsMarker;
    final Action<T> result = actions.actions[intent.runtimeType] as Action<T>;
    if (result != null) {
      action = result;
      actionElement = element;
      return true;
    }
    return false;
  });

  assert(() {
    if (nullOk) {
      return true;
    }
    if (action == null) {
      throw FlutterError('Unable to find an action for an Intent with type '
          '${intent.runtimeType} in an $Actions widget in the given context.\n'
          '$Actions.invoke() was unable to find an $Actions widget that '
          "contained a mapping for the given intent, or the intent type isn't the "
          'same as the type argument to invoke (which is $T - try supplying a '
          'type argument to invoke if one was not given)\n'
          'The context used was:\n'
          '  $context\n'
          'The intent type requested was:\n'
          '  ${intent.runtimeType}');
    }
    return true;
  }());
  // Invoke the action we found using the relevant dispatcher from the Actions
  // Element we found.
  return actionElement != null ? _findDispatcher(actionElement).invokeAction(action, intent, context) : null;
}