firebase - 在 Firestore Flutter 中计算值匹配的文档
问题描述
我得到了 20 条评论的价值,但我只在我的帖子上输入了两条评论,我没有找到这个解决方案。在上面我使用流生成器的错误中,我在哪里对我的文本进行查询。
流生成器:
StreamBuilder(
stream: FirebaseFirestore.instance.collection("post").snapshots(),
builder: (BuildContext context,AsyncSnapshot<QuerySnapshot>snapshot){
return ListView.builder(
shrinkWrap: true,
scrollDirection: Axis.vertical,
itemCount: snapshot.data.docs.length,
physics: NeverScrollableScrollPhysics(),
itemBuilder: (_,index) {
return
Text('${FirebaseFirestore.instance.collection("comments").where("postid",isEqualTo:
snapshot.data.docs[index].id).get().toString().length}')
}
解决方案
您必须.length
直接使用快照的 docs 属性,如下所示:
final QuerySnapshot snapshot = await Firestore.instance.collection('products').where("postid",isEqualTo: snapshot.data.docs[index].id).get();
final int documents = snapshot.docs.length;
使用 Future 的另一种方式:
FirebaseFirestore.instance
.collection("products")
.where("postid", isEqualTo: snapshot.data.docs[index].id)
.get()
.then((snapshot) => {
print(snapshot.docs.length);
});
推荐阅读
- apache - 在 wamp 上部署 symfony 4 演示应用程序
- java - SPQR GraphQL 中的持久查询
- dart - 使用 Flutter Slider 未正确触发 onChangeEnd
- javascript - 无法处理 JWTStrategy 回调中抛出的错误
- sql - 使用以“A”开头的主键的 SQL 创建表
- c# - 有没有办法使用相同的字段名称来 SetVisibleSignature?
- java - 使用 Web 服务获取 cookie 值中的用户名和密码
- bash - 如何从文本中提取特定字符串
- openssl - 为什么在将 pkcs8 密钥从 pem 转换为 der 时需要密码
- java - 创建脚本以重新启动主机名位于文本文件中的计算机