elementAt method

IndexedNode elementAt(
  1. String path
)
override
  • Utility method to get a child node at the path. Get any item at path from the root The keys of the items to be traversed should be provided in the path

For example in a TreeView like this

Node get mockNode1 => Node.root()
  ..addAll([
    Node(key: "0A")..add(Node(key: "0A1A")),
    Node(key: "0B"),
    Node(key: "0C")
      ..addAll([
        Node(key: "0C1A"),
        Node(key: "0C1B"),
        Node(key: "0C1C")
          ..addAll([
            Node(key: "0C1C2A")
              ..addAll([
                Node(key: "0C1C2A3A"),
                Node(key: "0C1C2A3B"),
                Node(key: "0C1C2A3C"),
              ]),
          ]),
      ]),
  ]);

In order to access the Node with key "0C1C", the path would be 0C.0C1C

Note: The root node ROOT_KEY does not need to be in the path

Implementation

IndexedNode elementAt(String path) {
  IndexedNode currentNode = this;
  for (final nodeKey in path.splitToNodes) {
    if (nodeKey == currentNode.key) {
      continue;
    } else {
      final index =
          currentNode.children.indexWhere((node) => node.key == nodeKey);
      if (index < 0)
        throw NodeNotFoundException(parentKey: path, key: nodeKey);
      final nextNode = currentNode.children[index];
      currentNode = nextNode;
    }
  }
  return currentNode;
}