fromMap static method

Tree fromMap(
  1. Map<String, dynamic> data, {
  2. String? root,
})

Creates a tree from a nested map structure.

Implementation

static Tree fromMap(Map<String, dynamic> data, {String? root}) {
  final tree = Tree();
  if (root != null) tree.root(root);

  void addItems(Tree t, Map<String, dynamic> map) {
    for (final entry in map.entries) {
      if (entry.value is Map<String, dynamic>) {
        final subtree = Tree()..root(entry.key);
        addItems(subtree, entry.value as Map<String, dynamic>);
        t.child(subtree);
      } else if (entry.value is List) {
        final subtree = Tree()..root(entry.key);
        for (final item in entry.value as List) {
          if (item is Map<String, dynamic>) {
            final nested = Tree();
            addItems(nested, item);
            subtree.child(nested);
          } else {
            subtree.child(item.toString());
          }
        }
        t.child(subtree);
      } else {
        t.child(entry.key);
      }
    }
  }

  addItems(tree, data);
  return tree;
}