of static method

FocusNode of(
  1. BuildContext context,
  2. {bool scopeOk = false,
  3. bool createDependency = true}
)

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 the context, then this method will throw an exception.

If createDependency is true (which is the default), calling this function creates a dependency that will rebuild the given context when the focus node gains or loses focus.

See also:

  • maybeOf, which is similar to this function, but will return null instead of throwing if it doesn't find a Focus node.

Implementation

static FocusNode of(BuildContext context, { bool scopeOk = false, bool createDependency = true }) {
  final FocusNode? node = Focus.maybeOf(context, scopeOk: scopeOk, createDependency: createDependency);
  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!;
}