elementAt method

Node 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

Node elementAt(String path) {
  Node currentNode = this;
  for (final nodeKey in path.splitToNodes) {
    if (nodeKey == currentNode.key) {
      continue;
    } else {
      final nextNode = currentNode.children[nodeKey];
      if (nextNode == null)
        throw NodeNotFoundException(parentKey: path, key: nodeKey);
      currentNode = nextNode;
    }
  }
  return currentNode;
}