of static method

FocusTraversalPolicy of(
  1. BuildContext context
)

Returns the FocusTraversalPolicy that applies to the FocusNode of the nearest ancestor Focus widget, given a BuildContext.

Will throw a FlutterError in debug mode, and throw a null check exception in release mode, if no Focus ancestor is found, or if no FocusTraversalPolicy applies to the associated FocusNode.

This function looks up the nearest ancestor Focus (or FocusScope) widget, and uses its FocusNode (or FocusScopeNode) to walk up the focus tree to find the applicable FocusTraversalPolicy for that node.

Calling this function does not create a rebuild dependency because changing the traversal order doesn't change the widget tree, so nothing needs to be rebuilt as a result of an order change.

The FocusTraversalPolicy is set by introducing a FocusTraversalGroup into the widget tree, which will associate a policy with the focus tree under the nearest ancestor Focus widget.

See also:

Implementation

static FocusTraversalPolicy of(BuildContext context) {
  final FocusTraversalPolicy? policy = maybeOf(context);
  assert(() {
    if (policy == null) {
      throw FlutterError(
        'Unable to find a Focus or FocusScope widget in the given context, or the FocusNode '
        'from with the widget that was found is not associated with a FocusTraversalPolicy.\n'
        'FocusTraversalGroup.of() was called with a context that does not contain a '
        'Focus or FocusScope widget, or there was no FocusTraversalPolicy in effect.\n'
        'This can happen if there is not a FocusTraversalGroup that defines the policy, '
        'or if the context comes from a widget that is above the WidgetsApp, MaterialApp, '
        'or CupertinoApp widget (those widgets introduce an implicit default policy) \n'
        'The context used was:\n'
        '  $context',
      );
    }
    return true;
  }());
  return policy!;
}