transformListToMap static method

Map<String, dynamic> transformListToMap(
  1. List dataList,
  2. Config config
)

@params @desc List to map

Implementation

static Map<String, dynamic> transformListToMap(List dataList, Config config) {
  Map obj = {};
  int? rootId;
  dataList.forEach((v) {
    // 根节点
    if (v[config.parentId] != 0) {
      if (obj[v[config.parentId]] != null) {
        if (obj[v[config.parentId]][config.children] != null) {
          obj[v[config.parentId]][config.children].add(v);
        } else {
          obj[v[config.parentId]][config.children] = [v];
        }
      } else {
        obj[v[config.parentId]] = {
          config.children: [v],
        };
      }
    } else {
      rootId = v[config.id];
    }
    if (obj[v[config.id]] != null) {
      v[config.children] = obj[v[config.id]][config.children];
    }
    obj[v[config.id]] = v;
  });
  return obj[rootId] ?? {};
}