繁体   English   中英

Flutter-Firestore:一次更新多个文档

[英]Flutter-Firestore: Update Multiple Docs At Once

我是 Flutter 的新手,我在这上面花了一些时间,但找不到我想做的事情的明确答案。 我正在查询一个集合中的几个文档,然后尝试使用batch function 更新它们。但是,我无法理解文档和之前关于如何在 Flutter 中使用带有多个文档的 Firestore 中的batch function 的答案。 查询似乎正在运行并返回文档,但是batch.update()抛出了一个我无法弄清楚如何解决的错误。 这是我所拥有的

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();
)

更新:我越来越近了,但现在它只更新了批处理的第一个文档,而不是 rest。它给出了下面的错误代码片段。

[VERBOSE-2:dart_vm_initializer.cc(41)] Unhandled Exception: Bad state: This batch has already been committed and can no longer be changed.

在我的头撞墙几个小时之后,这是在查询多个文档后最终使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM