toTree<D, T extends TreeNode<T>> function

T toTree<D, T extends TreeNode<T>>(
  1. D data,
  2. List<D> childrenCallback(
    1. D
    ),
  3. T build(
    1. T?,
    2. D
    ), {
  4. int deep = 0,
  5. T? parent,
  6. int sort(
    1. T,
    2. T
    )?,
})

Implementation

T toTree<D, T extends TreeNode<T>>(
  D data,
  List<D> Function(D) childrenCallback,
  T Function(T?, D) build, {
  int deep = 0,
  T? parent,
  int Function(T, T)? sort,
}) {
  T root = build.call(parent, data);
  root._deep = deep;
  root.parent = parent;
  for (var child in childrenCallback.call(data)) {
    root.add(toTree<D, T>(child, childrenCallback, build, deep: deep + 1, parent: root));
  }
  if (sort != null) {
    root._childrenList.sort((a, b) {
      return sort.call(a, b);
    });
  }
  return root;
}