of method Null safety
- BuildContext context,
- {bool scopeOk = false}
Returns the focusNode of the Focus that most tightly encloses the given BuildContext.
If no Focus node is found before reaching the nearest FocusScope widget, or there is no Focus widget in scope, then this method will throw an exception.
The context
and scopeOk
arguments must not be null.
Calling this function creates a dependency that will rebuild the given context when the focus changes.
See also:
Implementation
static FocusNode of(BuildContext context, { bool scopeOk = false }) {
assert(context != null);
assert(scopeOk != null);
final _FocusMarker? marker = context.dependOnInheritedWidgetOfExactType<_FocusMarker>();
final FocusNode? node = marker?.notifier;
assert(() {
if (node == null) {
throw FlutterError(
'Focus.of() was called with a context that does not contain a Focus widget.\n'
'No Focus widget ancestor could be found starting from the context that was passed to '
'Focus.of(). This can happen because you are using a widget that looks for a Focus '
'ancestor, and do not have a Focus widget descendant in the nearest FocusScope.\n'
'The context used was:\n'
' $context',
);
}
return true;
}());
assert(() {
if (!scopeOk && node is FocusScopeNode) {
throw FlutterError(
'Focus.of() was called with a context that does not contain a Focus between the given '
'context and the nearest FocusScope widget.\n'
'No Focus ancestor could be found starting from the context that was passed to '
'Focus.of() to the point where it found the nearest FocusScope widget. This can happen '
'because you are using a widget that looks for a Focus ancestor, and do not have a '
'Focus widget ancestor in the current FocusScope.\n'
'The context used was:\n'
' $context',
);
}
return true;
}());
return node!;
}