checkNodeHasAncestor method

bool checkNodeHasAncestor({
  1. required T node,
  2. required T potentialAncestor,
  3. bool checkForEquality = false,
})

Checks if potentialAncestor is present in the path from node to its root node.

By default, node is not checked against potentialAncestor. Set checkForEquality to true so an additional node == potentialAncestor check is done.

This method requires a parentProvider to be defined and will throw an AssertionError in debug mode.

Implementation

bool checkNodeHasAncestor({
  required T node,
  required T potentialAncestor,
  bool checkForEquality = false,
}) {
  assert(_debugCheckHasParentProvider());

  if (checkForEquality && node == potentialAncestor) {
    return true;
  }

  T? current = parentProvider(node);
  bool foundAncestor = false;

  while (!(foundAncestor || current == null)) {
    foundAncestor = current == potentialAncestor;
    current = parentProvider(current);
  }

  return foundAncestor;
}