find<T extends Intent> static method

Action<T> find<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 assert in debug mode, and throw an exception in release mode.

Limitations:

It is strongly recommended that callers explicitly set the type parameter to Intent when the intent parameter is not null:

Actions.find<Intent>(context, intent: intent); // GOOD
Actions.find(context, intent: intent); // BAD

If the type parameter is not set to Intent when the intent parameter is not null, this method might be unable to return a perfectly capable Action. For instance, this method cannot return an Action<Intent> - an action that can be bound to any intent - unless T is exactly Intent. This will trigger assertions in debug mode.

See also:

  • maybeFind, which is similar to this function, but will return null if no Actions ancestor is found.

Implementation

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

  assert(() {
    if (action == null) {
      final Type type = intent?.runtimeType ?? T;
      throw FlutterError(
        'Unable to find an action for a $type in an $Actions widget '
        'in the given context.\n'
        "$Actions.find() was called on a context that doesn't contain an "
        '$Actions widget with a mapping for the given intent type.\n'
        'The context used was:\n'
        '  $context\n'
        'The intent type requested was:\n'
        '  $type',
      );
    }
    return true;
  }());
  return action!;
}