firebase - Flutter firebase 查询快照错误:“未处理的异常:错误状态:无元素”
问题描述
获取具有必填字段值的文档,但这不起作用
await FirebaseFirestore.instance
.collection('orders')
.where("id", isEqualTo: orderList[index].id)
.get()
.then((snapshot) {
snapshot.docs.first.reference.update({"status": 'Rejected'});
print("yes");
});
但这有效
await FirebaseFirestore.instance
.collection('orders')
.where("id", isEqualTo: orderList[index].id)
.get()
.then((snapshot) {
//snapshot.docs.first.reference.update({"status": 'Rejected'});
print("yes");
});
错误
E/flutter (10845): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] 未处理异常:错误状态:无元素
解决方案
查询返回的 QuerySnapshot 对象可以返回 0 个或多个 DocumentSnapshot 对象。docs
在索引到数组之前,您应该检查是否有超过 0 个。
await FirebaseFirestore.instance
.collection('orders')
.where("id", isEqualTo: orderList[index].id)
.get()
.then((snapshot) {
if (snapshot.docs.length > 0) {
snapshot.docs.first.reference.update({"status": 'Rejected'});
}
else {
// Figure out what you want to do if the list is empty.
// This means your query matched no documents.
}
});
推荐阅读
- c - 不传递参数比较函数如何工作
- android - 缺少api密钥android Cloudinary上传错误
- reactjs - RadioGroup 未更新为选定的单选按钮
- c# - 如何有效地处理多个按钮和其他元素的可见性和 isEnabled
- algorithm - 有人可以直观地解释一下这段代码吗?
- matlab - 是否有一个 Matlab 函数可以将 rho 和 theta 重新投影到投影的纬度?
- react-native - 与 RN 0.60 自动链接不兼容
- c++ - 如何读取特定单词之间的文本文件?
- reactjs - 我无法从路径访问“/login”
- sql-server - 解决 Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException 的问题