getNamePath static method

Iterable<String>? getNamePath(
  1. Node ancestor,
  2. Node child
)

Returns the sequence of Node.name values that walks from ancestor down to child through the scene graph.

Useful for serializing a stable reference to a descendant node that can later be resolved with getChildByNamePath.

Returns null (and prints a debug warning) if ancestor is not an actual ancestor of child.

Implementation

static Iterable<String>? getNamePath(Node ancestor, Node child) {
  List<String> result = [];
  Node? current = child;
  while (current != null) {
    if (identical(current, ancestor)) {
      return result.reversed;
    }
    result.add(current.name);
    current = current._parent;
  }

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