maybeFind<T extends Intent> static method
- BuildContext context, {
- 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.
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:
Implementation
static Action<T>? maybeFind<T extends Intent>(BuildContext context, {T? intent}) {
Action<Intent>? action;
_visitActionsAncestors(context, (InheritedElement element) {
final actions = element.widget as _ActionsScope;
final Action<Intent>? result = _getActionForIntent<T>(actions, intent);
if (result != null) {
context.dependOnInheritedElement(element);
action = result;
return true;
}
return false;
});
if (action case final Action<T>? action) {
return action;
}
assert(() {
throw FlutterError.fromParts(<DiagnosticsNode>[
ErrorSummary('An ${action.runtimeType} cannot be cast to an Action<$T>.'),
ErrorDescription(
'A valid action $action was found but could not be returned by Actions.maybeFind<$T>.',
),
ErrorHint(
'This is a current limitation of the Actions widget, '
'see https://github.com/flutter/flutter/issues/180871 for more details. '
'As a workaround, consider using Actions.invoke or Actions.maybeInvoke instead, '
'or explicitly set the type parameter to Intent: '
'Actions.maybeFind<Intent>(context, intent)',
),
]);
}());
return null;
}