updateNode method

List<Node<T>> updateNode(
  1. String key,
  2. Node<T> newNode, {
  3. Node<T>? parent,
})

Updates an existing node identified by specified key. This method returns a new list with the updated node.

Implementation

List<Node<T>> updateNode(String key, Node<T> newNode, {Node<T>? parent}) {
  List<Node<T>> _children = parent == null ? this.children : parent.children;
  return _children.map((Node<T> child) {
    if (child.key == key) {
      return newNode;
    } else {
      if (child.isParent) {
        return child.copyWith(
          children: updateNode(
            key,
            newNode,
            parent: child,
          ),
        );
      }
      return child;
    }
  }).toList();
}