[英]Flutter-Firestore: Update Multiple Docs At Once
I am pretty new to Flutter and I have spent some time on this and cannot find a clear answer for what I am trying to do.我是 Flutter 的新手,我在这上面花了一些时间,但找不到我想做的事情的明确答案。 I am querying a collection for a couple of docs and then trying to update them using the
batch
function. However, I am having trouble understanding the documentation and previous answers on how exactly to use the batch
function in Flutter with Firestore with multiple docs.我正在查询一个集合中的几个文档,然后尝试使用
batch
function 更新它们。但是,我无法理解文档和之前关于如何在 Flutter 中使用带有多个文档的 Firestore 中的batch
function 的答案。 The query seems to be working and returning the docs, but the batch.update()
is throwing an error that I cannot figure out how to solve.查询似乎正在运行并返回文档,但是
batch.update()
抛出了一个我无法弄清楚如何解决的错误。 Here is what I have这是我所拥有的
InkWell(
onTap: () async {
final batch = FirebaseFirestore.instance.batch();
var pushNotifications = await FirebaseFirestore.instance
.collection('collection name')
.where(first filter)
.where(second filter)
.get();
print(pushNotifications.docs.length);
//this is where the error is occuring when trying to update the batch
for (var postDocs in pushNotificiations.docs) {
batch.update(postDocs.reference, {
"user_refs" : usersInchallenge
}
);
await batch.commit();
)
UPDATE: I am getting closer but now it is only updating the first document of the batch and not the rest. It is giving the error below with the udpated code snippet.更新:我越来越近了,但现在它只更新了批处理的第一个文档,而不是 rest。它给出了下面的错误代码片段。
[VERBOSE-2:dart_vm_initializer.cc(41)] Unhandled Exception: Bad state: This batch has already been committed and can no longer be changed.
After banging my head against a wall for a few hours, here is what finally worked with batch()
after querying multiple docs.在我的头撞墙几个小时之后,这是在查询多个文档后最终使用
batch()
的方法。
final batch = FirebaseFirestore.instance.batch();
var pushNotificiations = await FirebaseFirestore.instance
.collection('ff_user_push_notifications')
.where(filter 1)
.where(filter 2)
.get();
print(pushNotificiations.docs.length);
pushNotificiations.docs.forEach((doc) => {
batch.update(doc.reference, {
"user_refs" : usersInchallenge
}),
});
batch.commit();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.