颤振火库查询快照错误:"Unhandled Exception: Bad state: No element"



获取具有所需字段值的文档,但这不适用于

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(:[错误:flutter/lib/ui/ui_start_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.
}
});

相关内容

最新更新