of method

FocusNode of (
  1. BuildContext context,
  2. {bool nullOk: false,
  3. 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. To return null instead of throwing, pass true for nullOk.

The context and nullOk arguments must not be null.

Calling this function creates a dependency that will rebuild the given context when the focus changes.

Implementation

static FocusNode of(BuildContext context, { bool nullOk = false, bool scopeOk = false }) {
  assert(context != null);
  assert(nullOk != null);
  assert(scopeOk != null);
  final _FocusMarker marker = context.dependOnInheritedWidgetOfExactType<_FocusMarker>();
  final FocusNode node = marker?.notifier;
  if (node == null) {
    if (!nullOk) {
      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 null;
  }
  if (!scopeOk && node is FocusScopeNode) {
    if (!nullOk) {
      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 null;
  }
  return node;
}