visitAncestorElements method

  1. @override
void visitAncestorElements(
  1. bool visitor(
    1. Element element
    )
)
override

Walks the ancestor chain, starting with the parent of this build context's component, invoking the argument for each ancestor. The callback is given a reference to the ancestor component's corresponding Element object. The walk stops when it reaches the root component or when the callback returns false. The callback must not return null.

This is useful for inspecting the component tree.

Calling this method is relatively expensive (O(N) in the depth of the tree).

Implementation

@override
void visitAncestorElements(bool Function(Element element) visitor) {
  Element? ancestor = _parent;
  while (ancestor != null && visitor(ancestor)) {
    ancestor = ancestor._parent;
  }
}