如何从文档 firestore flutter 获取子集合

发布于 2025-01-09 17:43:14 字数 2083 浏览 1 评论 0原文

我试图从另一个文档中的子集合中获取文档,当我尝试获取文档并使用“文档数据填充本地列表时“它没有填满它,有人能告诉我我在这里做错了什么吗?

我尝试获取子集合时的方法:

  static Stream<List<CheckInOutModel>> employeeCheckInOutStream() {
    return firebaseFirestore
        .collection('employees')
        .doc(auth.currentUser!.uid)
        .collection('employeeList')
        .snapshots()
        .asyncMap((QuerySnapshot querySnapshot) {
      final List<CheckInOutModel> employeesCheckInOutList = [];
      for (final element in querySnapshot.docs) {
        firebaseFirestore
            .collection('employees')
            .doc(auth.currentUser!.uid)
            .collection('employeeList')
            .doc(element.id)
            .collection('checkInOutList')
            .snapshots()
            .asyncMap((QuerySnapshot query) {
          for (final element in query.docs) {
            final employeeCheckInOutModel =
                CheckInOutModel.fromDocumentSnapshot(
              documentSnapshot: element,
            );
            employeesCheckInOutList.add(employeeCheckInOutModel);
          }
        });
      }
      return employeesCheckInOutList;
    });
  }

当我获取子集合所在文档的字段时我的方法:

  static Stream<List<EmployeeModel>> employeeStream() {
    return firebaseFirestore
        .collection('employees')
        .doc(auth.currentUser!.uid)
        .collection('employeeList')
        .snapshots()
        .map((QuerySnapshot query) {
      final List<EmployeeModel> employees = [];
      for (final employee in query.docs) {
        final employeeModel =
            EmployeeModel.fromDocumentSnapshot(documentSnapshot: employee);
        employees.add(employeeModel);
      }
      return employees;
    });
  }

在此处输入图像描述输入图片此处描述

Im trying to fetch the documents from a subcollection which is in another document, and when I try to fetch the docs and fill a local list with the "docs data" it doesn't fill it, can anyone tell me what I'm doing wrong here?

My method of when I try to fetch the subcollection:

  static Stream<List<CheckInOutModel>> employeeCheckInOutStream() {
    return firebaseFirestore
        .collection('employees')
        .doc(auth.currentUser!.uid)
        .collection('employeeList')
        .snapshots()
        .asyncMap((QuerySnapshot querySnapshot) {
      final List<CheckInOutModel> employeesCheckInOutList = [];
      for (final element in querySnapshot.docs) {
        firebaseFirestore
            .collection('employees')
            .doc(auth.currentUser!.uid)
            .collection('employeeList')
            .doc(element.id)
            .collection('checkInOutList')
            .snapshots()
            .asyncMap((QuerySnapshot query) {
          for (final element in query.docs) {
            final employeeCheckInOutModel =
                CheckInOutModel.fromDocumentSnapshot(
              documentSnapshot: element,
            );
            employeesCheckInOutList.add(employeeCheckInOutModel);
          }
        });
      }
      return employeesCheckInOutList;
    });
  }

My method when I fetch the fields of the documents that the subcollection is in:

  static Stream<List<EmployeeModel>> employeeStream() {
    return firebaseFirestore
        .collection('employees')
        .doc(auth.currentUser!.uid)
        .collection('employeeList')
        .snapshots()
        .map((QuerySnapshot query) {
      final List<EmployeeModel> employees = [];
      for (final employee in query.docs) {
        final employeeModel =
            EmployeeModel.fromDocumentSnapshot(documentSnapshot: employee);
        employees.add(employeeModel);
      }
      return employees;
    });
  }

enter image description here
enter image description here

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

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

发布评论

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

评论(1

北笙凉宸 2025-01-16 17:43:14

所以我弄清楚我在这里做错了什么,我尝试在调用回调时只需要它时调用它的流,所以我相应地更改了逻辑并使用 Future 而不是 流式传输

我更新的代码:

  static Future<List<CheckInOutModel>> employeeCheckInOutStream({
    required String id,
  }) async {
    final List<CheckInOutModel> employeesCheckInOutList = [];
    final query = await firebaseFirestore
        .collection('employees')
        .doc(auth.currentUser!.uid)
        .collection('employeeList')
        .doc(id)
        .collection('checkInOutList')
        .get();

    for (final employee in query.docs) {
      final employeeCheckInOutModel = CheckInOutModel.fromDocumentSnapshot(
        documentSnapshot: employee,
      );
      employeesCheckInOutList.add(employeeCheckInOutModel);
    }

    return employeesCheckInOutList;
  }

So I figured out what I did wrong here, I tried to call a stream of it when I only needed it when a callBack is called, so I changed the logic accordingly and went with Future instead Stream

My updated code:

  static Future<List<CheckInOutModel>> employeeCheckInOutStream({
    required String id,
  }) async {
    final List<CheckInOutModel> employeesCheckInOutList = [];
    final query = await firebaseFirestore
        .collection('employees')
        .doc(auth.currentUser!.uid)
        .collection('employeeList')
        .doc(id)
        .collection('checkInOutList')
        .get();

    for (final employee in query.docs) {
      final employeeCheckInOutModel = CheckInOutModel.fromDocumentSnapshot(
        documentSnapshot: employee,
      );
      employeesCheckInOutList.add(employeeCheckInOutModel);
    }

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