Georgios
Georgios

Reputation: 1037

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

Upvotes: 0

Views: 76

Answers (1)

Stewie Griffin
Stewie Griffin

Reputation: 5608

What you expect and what you return is not the same type. I'm assuming that you want to receive a stream which is Stream<List<Unavailability>. In this case, you could use StreamTransformer to change the type of the stream from Firestore, so you can get what you want. However, I don't know if your method will work because you don't use unavailabilities anywhere. Moreover, I don't know what UnavailabilityEntity.fromSnapshot is for. Assuming you try to parse the data. Try to use this code below. At least the error should disappear.

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

Upvotes: 1

Related Questions