requestFocus method Null safety

void requestFocus(
  1. [FocusNode? node]
)

Requests the primary focus for this node, or for a supplied node, which will also give focus to its ancestors.

If called without a node, request focus for this node. If the node hasn't been added to the focus tree yet, then defer the focus request until it is, allowing newly created widgets to request focus as soon as they are added.

If the given node is not yet a part of the focus tree, then this method will add the node as a child of this node before requesting focus.

If the given node is a FocusScopeNode and that focus scope node has a non-null FocusScopeNode.focusedChild, then request the focus for the focused child. This process is recursive and continues until it encounters either a focus scope node with a null focused child or an ordinary (non-scope) FocusNode is found.

The node is notified that it has received the primary focus in a microtask, so notification may lag the request by up to one frame.

Implementation

void requestFocus([FocusNode? node]) {
  if (node != null) {
    if (node._parent == null) {
      _reparent(node);
    }
    assert(node.ancestors.contains(this), 'Focus was requested for a node that is not a descendant of the scope from which it was requested.');
    node._doRequestFocus(findFirstFocus: true);
    return;
  }
  _doRequestFocus(findFirstFocus: true);
}