简体   繁体   English

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

[英]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.

相关问题 Flutter-Firestore:- 无法将图像上传到 Firestore。 它说找不到参考 object - Flutter-Firestore:- Unable to upload image to firestore. its says reference object is not found 如何分配列表<dynamic>列出<khana> ,其中 Khana 是 model class 在 Flutter-Firestore 中的一个参数?</khana></dynamic> - How to assign List<dynamic> to List<Khana>, where Khana is a model class in a parameter in Flutter-Firestore? Flutter Firestore - 仅获取 10 个带有 ID 的文档 - Flutter Firestore - get only 10 docs with their id Firebase Firestore get all Docs 在 Flutter 中为空 - Firebase Firestore get all Docs is empty in Flutter Flutter 多个 Firestore 查询 - Flutter Multiple Firestore Queries Flutter - Firestore - 列出来自集合字段的字符串,并在按下“保存设置”后更新 Fire Store - Flutter - Firestore - List String to be from collection field and update fire store once "Save Setting' is pressed 如何在 Firestore 中一次获取一组文档 ID 的所有文档,但只获取这些文档中的部分属性,而不是整个文档? - How to get all documents at once for a set of doc IDs, but only part of the properties in those docs, not the entire docs, in Firestore? Flutter/Firestore - 查询快照不包括所有文档 - Flutter/Firestore - query snapshot does not include all docs 如何在 Firestore 集合组查询 Flutter 中获取子集合文档 - How to Get Subcollection docs in Firestore Collection Group Query Flutter Firestore 如何访问文档中的子集合 map (Flutter) - Firestore how to access subcollection inside docs map (Flutter)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM