getIndexPath static method

Iterable<int>? getIndexPath(
  1. Node ancestor,
  2. Node child
)

Returns the index lookup path from the ancestor node to the child node.

Implementation

static Iterable<int>? getIndexPath(Node ancestor, Node child) {
  List<int> result = [];
  Node? current = child;
  while (current != null) {
    if (identical(current, ancestor)) {
      return result.reversed;
    }
    if (current._parent == null) {
      break;
    }
    result.add(current._parent!.children.indexOf(current));
    current = current._parent;
  }

  debugPrint(
      'Index path formation failed because the given ancestor was not an ancestor of the given child.');
  return null;
}