I keep tree view data on my firebase. I am receiving this data as a stream. The data is kept in firebase with the ProductCategory model. Within the ProdutCategory model, children data is kept as Stream. I need to convert this data to List .
I wrote the following codes for this. However, although it works, it cannot notice the changes in children and works very slowly.
I'm waiting for your suggestions, thank you
Future<List<TreeViewNode>> transform(
Stream<List<ProductCategory>> streamLPC) async {
List<ProductCategory> lp = await streamLPC.first;
List<TreeViewNode> lt = [];
for (var i = 0; i < lp.length; i++) {
ProductCategory p = lp[i];
List<TreeViewNode> ct = [];
if (p.childProductCategories != null) {
ct = await transform(p.childProductCategories);
}
TreeViewNode t = TreeViewNode(
key: p.id,
label: p.name,
children: ct,
);
lt.add(t);
}
return lt;
}
Stream<List<TreeViewNode>> getStreamTreeViewNodeList(
Stream<List<ProductCategory>> streamProductCategoryList) {
StreamTransformer streamTransformer = StreamTransformer<
List<ProductCategory>, List<TreeViewNode>>.fromHandlers(
handleData: (List<ProductCategory> listPC,
EventSink<List<TreeViewNode>> listTN) async {
List<TreeViewNode> lt = [];
for (var i = 0; i < listPC.length; i++) {
List<TreeViewNode> c =
await transform(listPC[i].childProductCategories);
ProductCategory p = listPC[i];
TreeViewNode t = TreeViewNode(key: p.id, label: p.name, children: c);
lt.add(t);
}
listTN.add(lt);
});
return streamProductCategoryList.transform(streamTransformer);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…