allChildrenOf method

List<T> allChildrenOf(
  1. T e
)

Gets a flat list of T when given a parent of type T.

Implementation

List<T> allChildrenOf(T e) {
  List<T> allChildren = <T>[];
  Queue<T> parentsToCheck = Queue<T>();

  // To prevent an accidental circular reference
  Set<T> visited = <T>{};

  visited.add(e);
  if (hasChildren(e)) {
    parentsToCheck.add(e);
  }

  Map<T, List<OptionGroup<T>>> hierarchyMap = getHierarchyMap();

  while (parentsToCheck.isNotEmpty) {
    T parent = parentsToCheck.removeFirst();

    hierarchyMap[parent]!.forEach((optionGroup) {
      optionGroup.toList().forEach((item) {
        if (!visited.contains(item)) {
          allChildren.add(item);
          visited.add(item);
          if (hasChildren(item)) {
            parentsToCheck.add(item);
          }
        }
      });
    });
  }

  return allChildren;
}