首页 > 解决方案 > Fetch all documents and their sub-collections

问题描述

I am coding an employee scheduling app in Flutter and I am using Firestore to store my data.

I have a collection of Employees in which there are saved basic employee parameters such as name, salary, designation, email.

For each employee document, I have a sub-collection containing their unavailability. The unavailability might be due to being already assigned to a shift, sickness, vacation.

For fetching the data, I implemented a firebase_employees_repository. Within the repository, I have the following function that gets the employees in realtime.

  @override
  Stream<List<Employee>> employees() {
    return employeeCollection.snapshots().map((snapshot) {
      return snapshot.documents
          .map((doc) => Employee.fromEntity(EmployeeEntity.fromSnapshot(doc)))
          .toList();
    });
  }

Now I want to do the same with the unavailabilities.

As far as my understanding, the unavailability list of an employee should get saved within the employee object.

I need the unavailability list of an employee, to show its status for a day/week/month. Also, when creating a new shift and wanting to assign to an employee, only the available should be shown.

This is what could be a solution, but I am getting the following error:

A value of type 'Future< List< Iterable< Unavailability>>>' can't be assigned to a variable of type 'Stream< List< Unavailability>>'. Try changing the type of the variable, or casting the right-hand type to 'Stream< List< Unavailability>>'.

  @override
  Stream<List<Employee>> employees() {
    return employeeCollection.snapshots().map((snapshot) {
      return snapshot.documents
          .map((doc) {

            Stream<List<Unavailability>> unavailabilities = 
                employeeCollection
                .document(doc.documentID)
                .collection('Unavailabilities')
                .snapshots().map((snapshot) {
                  return snapshot.documents
                      .map((innerDoc) => Unavailability.fromEntity(UnavailabilityEntity.fromSnapshot(innerDoc)));
                }).toList()
                ;

            return Employee.fromEntity(EmployeeEntity.fromSnapshot(doc));})
          .toList();
    });
  }

The whole project can be found in Github under this link: schedulingapp

标签: fluttergoogle-cloud-firestore

解决方案


你所期望的和你返回的不是同一种类型。我假设你想接收一个流是Stream<List<Unavailability>. 在这种情况下,您可以使用StreamTransformer从 Firestore 更改流的类型,这样您就可以获得所需的内容。但是,我不知道您的方法是否有效,因为您不在unavailabilities任何地方使用。此外,我不知道UnavailabilityEntity.fromSnapshot是为了什么。假设您尝试解析数据。尝试在下面使用此代码。至少错误应该消失。

Stream<List<Unavailability>> unavailabilities = employeeCollection
    .document(doc.documentID)
    .collection('Unavailabilities')
    .snapshots()
    .transform<List<Unavailability>>(
  StreamTransformer.fromHandlers(
    handleData: (QuerySnapshot data, EventSink<List<Unavailability>> sink) {
      sink.add(
        data.documents.map(
              (innerDoc) => Unavailability.fromEntity(
            UnavailabilityEntity.fromSnapshot(innerDoc),
          ),
        ).toList(),
      );
    },
  ),
);

推荐阅读