maybeFind<T extends Intent> method Null safety

Action<T>? maybeFind<T extends Intent>(
  1. BuildContext context,
  2. {T? intent}
)

Finds the Action bound to the given intent type T in the given context.

Creates a dependency on the Actions widget that maps the bound action so that if the actions change, the context will be rebuilt and find the updated action.

The optional intent argument supplies the type of the intent to look for if the concrete type of the intent sought isn't available. If not supplied, then T is used.

If no Actions widget surrounds the given context, this function will return null.

See also:

  • find, which is similar to this function, but will throw if no Actions ancestor is found.

Implementation

static Action<T>? maybeFind<T extends Intent>(BuildContext context, { T? intent }) {
  Action<T>? action;

  // Specialize the type if a runtime example instance of the intent is given.
  // This allows this function to be called by code that doesn't know the
  // concrete type of the intent at compile time.
  final Type type = intent?.runtimeType ?? T;
  assert(
    type != Intent,
    'The type passed to "find" resolved to "Intent": either a non-Intent '
    'generic type argument or an example intent derived from Intent must be '
    'specified. Intent may be used as the generic type as long as the optional '
    '"intent" argument is passed.',
  );

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

  return action;
}