【问题标题】:How to get a sub collection after using .where in flutter在颤振中使用.where后如何获取子集合
【发布时间】:2019-08-30 02:57:42
【问题描述】:
我想知道在使用 .where 后是否有办法获取子集合
这是我的数据库格式
Firestore.instance.collection("Users")
.where("followers", arrayContains: id) //id is the users id
//He i want to get the collection of posts that the particular user has posted
【问题讨论】:
标签:
android
firebase
flutter
google-cloud-firestore
【解决方案1】:
子集合位于单个文档下,而查询标识一组文档。如果当您知道查询只识别一个文档时,Firestore 客户端不知道这一点,即使知道,它也不知道该文档的完整路径。
你首先需要执行查询,然后获取单个文档,然后你可以获取每个文档的子集合。
【解决方案2】:
我正在尝试做类似的事情 -
Events -> widget.mydatahere(文档)-> eventFood(子集合)-> selectedFood(子集合中的文档)-> selectedFoodRecipe(foodRecipe 的值)。
这将使它工作,但是,这里的问题是即使 setstate,也不会更新 ui,控制台日志正常,但 ui 不更新。
Future<String> loadmyFoodItem() async {
await Firestore.instance
.collection('Events')
.where("eventName", isEqualTo: widget.mydatahere)
.getDocuments()
.then((onValue){
onValue.documents.forEach((f){
Firestore.instance.collection('Events').document(f.documentID)
.collection('eventFood')
.where('foodName', isEqualTo: selectedFood)
.getDocuments()
.then((querySnapshot){
querySnapshot.documents.forEach((result){
selectedFoodRecipe = result.data['foodRecipe'];
print("selectedFoodRecipe $selectedFoodRecipe");
});
});
});
});
return 'ready';
}