invoke<T extends Intent> method
Null safety
- BuildContext context,
- 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.
The context
and intent
arguments must not be null.
If the given intent
doesn't map to an action, then it will look to the
next ancestor Actions widget in the hierarchy until it reaches the root.
This method will throw an exception if no ambient Actions widget is found, or when a suitable Action is found but it returns false for Action.isEnabled.
Implementation
static Object? invoke<T extends Intent>(
BuildContext context,
T intent,
) {
assert(intent != null);
assert(context != null);
Object? returnValue;
final bool actionFound = _visitActionsAncestors(context, (InheritedElement element) {
final _ActionsMarker actions = element.widget as _ActionsMarker;
final Action<T>? result = _castAction(actions, intent: intent);
if (result != null && result.isEnabled(intent)) {
// Invoke the action we found using the relevant dispatcher from the Actions
// Element we found.
returnValue = _findDispatcher(element).invokeAction(result, intent, context);
}
return result != null;
});
assert(() {
if (!actionFound) {
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;
}());
return returnValue;
}