maybeInvoke<T extends Intent> method Null safety

Object? maybeInvoke<T extends Intent>(
  1. BuildContext context,
  2. T intent
)

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

This method returns the result of invoking the action's Action.invoke method. If no action mapping was found for the specified intent, or if the actions that were found were disabled, or the action itself returns null from Action.invoke, then this method returns null.

The context and intent arguments must not be null.

If the given intent doesn't map to an action, or doesn't map to one that returns true for Action.isEnabled in an Actions.actions map it finds, then it will look to the next ancestor Actions widget in the hierarchy until it reaches the root.

Implementation

static Object? maybeInvoke<T extends Intent>(
  BuildContext context,
  T intent,
) {
  assert(intent != 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) {
      actionElement = element;
      if (result.isEnabled(intent)) {
        action = result;
        return true;
      }
    }
    return false;
  });

  if (actionElement == null || action == null) {
    return null;
  }
  // Invoke the action we found using the relevant dispatcher from the Actions
  // Element we found.
  return _findDispatcher(actionElement!).invokeAction(action!, intent, context);
}