0

I have this piece of code, I want to return a stream of documentshapshots.

/* Get Firestore products docs
 */
  Stream<List<DocumentSnapshot>> fetchFirstProductListStream() {
    
    ....
   return  getFirestoreUserStream(loggedInUser.uid).map((UserModel rad) => 
    (geo
          .collection(collectionRef: collectionReference)
          .within(center: center, radius: rad.radius, field: field)) as List<DocumentSnapshot>
    );
}

I do get the following error as I try to cast the return value to List.

> [VERBOSE-2:ui_dart_state.cc(186)] Unhandled Exception: type
> '_AsBroadcastStream<List<DocumentSnapshot>>' is not a subtype of type > > 'List<DocumentSnapshot>'
LearnToday
  • 2,762
  • 9
  • 38
  • 67

1 Answers1

2

You have 2 options:

Option1: As you have finally converted the data into a plain list. return that directly. You dont need a Stream or a StreamBuilder to parse that now. You can directly use a ListView.builder() to render the data:

List<DocumentSnapshot> fetchFirstProductListStream() {
    
....
   return  getFirestoreUserStream(loggedInUser.uid).map((UserModel rad) => 
    (geo
          .collection(collectionRef: collectionReference)
          .within(center: center, radius: rad.radius, field: field)) as List<DocumentSnapshot>
    );
}

Option 2: Return a true Stream from the API and use a StreamBuilder to read it asynchronously:

Checkout this SO Post:

Sisir
  • 4,584
  • 4
  • 26
  • 37