maybeOf static method Null safety

FocusNode? maybeOf(
  1. BuildContext context,
  2. {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 return null.

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:

  • of, which is similar to this function, but will throw an exception if it doesn't find a Focus node instead of returning null.

Implementation

static FocusNode? maybeOf(BuildContext context, { bool scopeOk = false }) {
  assert(context != null);
  assert(scopeOk != null);
  final _FocusMarker? marker = context.dependOnInheritedWidgetOfExactType<_FocusMarker>();
  final FocusNode? node = marker?.notifier;
  if (node == null) {
    return null;
  }
  if (!scopeOk && node is FocusScopeNode) {
    return null;
  }
  return node;
}