firebase - 颤振:获取云火库中项目的长度
问题描述
我对flutter和firestore完全陌生。所以请帮忙。我想从userCarts中获取Firestore中拥有多少项目。在当前的uid中,userCart中有3个项目。所以我想得到结果为3
QuerySnapshot s= FirebaseFirestore.instance.collection('users').doc(user.uid).get();
int docs=s.docs.length;
解决方案
首先,当你打电话时,
QuerySnapshot s= FirebaseFirestore.instance.collection('users').doc(user.uid).get();
你没有得到 QuerySnapshot,你得到的是 DocumentSnapshot,你应该这样做
DocumentSnapshot doc= await FirebaseFirestore.instance.collection('users').doc(user.uid).get();
List<dynamic> userCart = doc.data()['userCart'];//here you already got the list
int length = userCart.length;
注意 - 我使用过await
,所以这应该在异步函数中。
推荐阅读
- php - 在 IF 语句中设置然后使用变量?
- c++ - 一种用于插入类对象的某些特定值的衬垫
- html - 通过单击它的父包装来选择输入单选
- c# - VS 安装项目 - RemovePreviousVersion 更改安装文件夹
- javascript - 纽约市与 webpack 4 使用 .tmp 文件夹
- asp.net - IIS (ASP.NET) 中的请求等待时间稳定
- javascript - 在php中使用json_encode编码数组后无法从json数组数据中提取变量值
- environment-variables - 从系统环境加载端点配置
- javascript - Visual Studio Code 中的变量引用
- regex - 如何在 Mongoose 查询中将变量与解析的电子邮件进行比较?