如何批量更新数组?

发布于 2025-01-11 18:36:37 字数 427 浏览 0 评论 0原文

因此,我正在创建一个出勤应用程序,其中有一个名为与会者的列表,其中包含所有参加讲座的学生的列表。

List<String> attendees = [uid0, uid1, uid2];

学生数据模型如下

Student(
List<String> attendance = [at0, at1]; //this is the list of lectures attended
String name;
)

,每个学生的文档名称是他们的用户ID
如何通过 batch() 函数更新每个学生的列表?

So I am creating an attendance app in which there is a list named attendees which contains the list of all students attending the lecture.

List<String> attendees = [uid0, uid1, uid2];

and the student data model is as follows

Student(
List<String> attendance = [at0, at1]; //this is the list of lectures attended
String name;
)

and the document name of every student is their user-id
How do I update the list of every student via the batch() function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不念旧人 2025-01-18 18:36:37

我假设您已经有一个包含 user-id 的与会者列表,正如您提到的 userId 将是 Firestore 中的 documentId 一样。

因此,您可以执行循环并直接更新您的文档,然后使用commit应用您的更改。

updateBatch(List<String> attendees) async {
  try {
    final db = FirebaseFirestore.instance;

    final WriteBatch batch = db.batch();

    // based on this doc https://firebase.flutter.dev/docs/firestore/usage/#batch-write
    // Each transaction or batch of writes can write to a maximum of 500 documents.
    for (final userid in attendees) {
      // Use the batch to update a document that exist otherwise it will throw error,
      // in case you want to create that doc, maybe you should use `set` instead
      // ref: https://pub.dev/documentation/cloud_firestore/latest/cloud_firestore/WriteBatch/update.html
      batch.update(
        //Give the ref of document.
        db.document('Path/to/firestore/document'),  // e.g: /Students/userId
        //And the data to update.
        {"key": value},
      );
    }

    await batch.commit();
    print('success');
  } catch (e) {
    print('error $e');
  }
}

一些注意事项:

I assume you already have a list of attendees that contains user-id as you mentioned the userId would be the documentId in Firestore.

Therefore, you can do a loop and update your doc directly and then use commit to apply your changes.

updateBatch(List<String> attendees) async {
  try {
    final db = FirebaseFirestore.instance;

    final WriteBatch batch = db.batch();

    // based on this doc https://firebase.flutter.dev/docs/firestore/usage/#batch-write
    // Each transaction or batch of writes can write to a maximum of 500 documents.
    for (final userid in attendees) {
      // Use the batch to update a document that exist otherwise it will throw error,
      // in case you want to create that doc, maybe you should use `set` instead
      // ref: https://pub.dev/documentation/cloud_firestore/latest/cloud_firestore/WriteBatch/update.html
      batch.update(
        //Give the ref of document.
        db.document('Path/to/firestore/document'),  // e.g: /Students/userId
        //And the data to update.
        {"key": value},
      );
    }

    await batch.commit();
    print('success');
  } catch (e) {
    print('error $e');
  }
}

A few notes:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文