flutter - 将firestore子集合字段添加到列表中
问题描述
我正在尝试将子集合的字段添加到评论列表中。我可以知道我应该如何在 //To add to List// 部分中做到这一点吗?
“评论”集合包含 2 个子集合,即“1”和“2”。'1' 和 '2' 都包含 4 个字段的映射。
以下是firestore的代码和截图:
List<dynamic> reviewsList = [];
Future _getReviews() async{
firestore.collection('shops').doc(widget.shop.id).collection('reviews').get()
.then((reviews){
reviews.docs.forEach((result) {
firestore.collection('shops').doc(widget.shop.id).collection('reviews').doc(result.id)
.get().then((reviewDocumentSnapshot) {
// To add to List //
});
});
});
}
解决方案
该问题与对异步的误解有关。将您的功能更改为
Future _getReviews() async{
var reviews = await firestore.collection('shops').doc(widget.shop.id).collection('reviews').get();
reviews.docs.forEach((result) {
var reviewDocumentSnapshot= await firestore.collection('shops').doc(widget.shop.id).collection('reviews').doc(result.id);
//add this snapshot to list.
reviewsList[your_object.fromJson(reviewDocumentSnapshot)];
});
}
你的模型类将是
class your_model {
String name;
String review;
int star;
String uid;
your_model({this.name, this.review, this.star, this.uid});
your_model.fromJson(Map<String, dynamic> json) {
name = json['name'];
review = json['review'];
star = json['star'];
uid = json['uid'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['name'] = this.name;
data['review'] = this.review;
data['star'] = this.star;
data['uid'] = this.uid;
return data;
}
}
推荐阅读
- java - 字符串中的 NullPointerException
- r - 在 R 中的 shapefile 上绘制空间数据集时,如何绘制空间数据集的坐标?
- python - 如何为自己更改 setter 和 getter 以便能够在 python 中处理类和组合
- java - 错误“未为 User 类型定义带有 DefaultPasswordEncoder() 的方法”Spring Boot 密码
- javascript - 再次使用组件做出反应
- xslt - 基于 xslt 中的一个元素将 xml 记录与所需的子元素分组
- ios - 将 iOS 应用上传到我的个人 Apple 开发者帐户
- visual-foxpro - 这个函数在 Visual Foxpro 中有什么作用?
- sql-server - 查询仅更改sql server中的最后一条记录值
- java - 如何在 Android Studio 中更新分数和星星?