我的firestore数据库中有两个集合; “查询”和“用户”。我的要求是从“查询”集合中获取有关特定标志的所有查询。完成后,我需要检查每个查询中的uid,并使用“用户”集合中的该uid查询用户的数据。在这种情况下,我将在Flutter中使用StreamBuilder来获取用户的名称和号码。下面是我的方法:
return new StreamBuilder(
stream: db.collection("queries").where("isAttended", isEqualTo: false).snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot){
if(!snapshot.hasData) return new Text("Loading...");
return new ListView(
children: snapshot.data.documents.map((document){
String name = '';
String number = '';
var doc = db.collection("users").document(document["uid"]).get();
doc.then((onValue) {
number = onValue.data['number'];
name = onValue.data['name'];
});
return new ListTile(
title: new Text(name),
subtitle: new Text(number),
);
}).toList(),
);
},
);
问题是onValue返回null。请以我上面指定的方式建议我的数据查询方法。 TIA。
最佳答案
开始吧,
这是您的 child 列表List<Widget> children = [];
将您的ListView
子代更改为ListView(children: children, ...)
然后像这样更新您的streambuilder,
if(!snapshot.hasData) return new Text("Loading...");
initChildren(snapshot); // here is important
return new ListView(...
initChildren
是initChildren(snapshot) async {
final list = await Future.wait(snapshot.data.documents.map((document) async {
String name = '';
String number = '';
var doc = await db.collection("users").document(document["uid"]).get();
doc.then((onValue) {
number = onValue.data['number'];
name = onValue.data['name'];
});
return new ListTile(
title: new Text(name),
subtitle: new Text(number),
);
}));
setState(() => children = list);
}
关于firebase - 用于列表构建的Flutter中的Firestore嵌套查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58248465/