getParent<T> method

Node<T>? getParent<T>(
  1. String key, {
  2. Node? parent,
})

Gets the parent of the node identified by specified key.

Implementation

Node<T>? getParent<T>(String key, {Node? parent}) {
  Node? _found;
  List<Node> _children = parent == null ? this.children : parent.children;
  Iterator iter = _children.iterator;
  while (iter.moveNext()) {
    Node child = iter.current;
    if (child.key == key) {
      _found = parent ?? child;
      break;
    } else {
      if (child.isParent) {
        _found = this.getParent<T>(key, parent: child);
        if (_found != null) {
          break;
        }
      }
    }
  }
  return _found == null ? null : _found as Node<T>;
}