Firestore type mismatch : The return type 'Stream<List<Flight>>' isn't a 'List<Flight>'

Viewed 27

In a flutter project, I am trying to return a list of objects as stream from a method. Here is my code:

Stream<List<Flight>> getLove(String seamanId) async* {
    yield* _firestore
        .collectionGroup(ConstStrings.seamans)
        .where(ConstStrings.uid, isEqualTo: seamanId)
        .limit(1)
        .snapshots()
        .map((querySnapshot) {
      final docref = querySnapshot.docs[0].reference;
      return docref.collection(ConstStrings.flights).snapshots().map(
            (snapshot) => (snapshot.docs
                .map((doc) => FlightDto.fromFirestore(doc).toDomain())
                .toList()),
          );
    });
  }

But getting the following compilation error :

The return type 'Stream<List<Flight>>' isn't a 'List<Flight>'

How to solve this?

1 Answers

Each time you call the snapshots() method, it returns a stream of updates from the database. Since you're calling snapshots() nested again in your code, you're essentially returning a Stream<Stream<List<Flight>>>.

To prevent this, you need to return a single event/snapshot from the inner read, either by taking a specific event from the stream (e.g. by calling first) or by using get() instead of snapshots() there.

Related