mapToTreeNodes method
Implementation
List<TreeNode> mapToTreeNodes(List<MockDataModel> permissions) {
final nodesMap = <String, TreeNode>{};
final rootNodes = <TreeNode>[];
for (final item in permissions) {
final paths = item.path?.trim().split(r'\\');
TreeNode? currentParent;
for (var i = 0; i < (paths?.length ?? 0); i++) {
final path = paths![i];
final hierarchyLevel = i;
final currentNode = nodesMap.putIfAbsent(path, () {
final newNode = TreeNode(
title: path,
code: path,
children: [],
hierarchy: hierarchyLevel,
parent: currentParent,
);
if (currentParent != null) {
currentParent.children.add(newNode);
} else {
rootNodes.add(newNode);
}
return newNode;
});
currentParent = currentNode;
}
final permissionNode = TreeNode(
title: item.name,
code: item.code,
parent: currentParent,
hierarchy: (currentParent?.hierarchy ?? 0) + 1,
children: [],
);
currentParent?.children.add(permissionNode);
}
return rootNodes;
}