0

My goal is to query Firebase and return a stream that automatically updates in case is added to the collection that fulfils the where() statement.

Stream<User> getUsers(String storeLocation) async* {
  Stream<QuerySnapshot<User?>> users = _firestore
      .collection(firebase_constants.kUsersCollection)
      .where('storeLocation', isEqualTo: storeLocation)
      .snapshots()
      .map((QuerySnapshot<Object?> querySnapshot) {
    return querySnapshot.docs.map((QueryDocumentSnapshot<Object?> doc) {
      return User.fromJson(doc.data()! as Map<String, dynamic>);
    }).toList();
    }).expand((users) => users);
}

The User class has a vanilla fromJson. The error that I have now

error: The return type 'List<User>' isn't a 'Iterable<QuerySnapshot<User?>>', as required by the closure's context. (return_of_invalid_type_from_closure at [firebase_data_repository] lib/src/user_repository.dart:45)

I could simply change the signature, but that wouldn't provide me with a stream.

Thanks for any help.

Stereo
  • 1,148
  • 13
  • 36

1 Answers1

0

It seems that this passes the linter, have to test whether it works as expected

Stream<User?> getUsers(String storeLocation) async* {
    final Stream<User?> users = _firestore
        .collection(firebase_constants.kUsersCollection)
        .where('storeLocation', isEqualTo: storeLocation)
        .snapshots()
        .map((QuerySnapshot<Object?> querySnapshot) {
      return querySnapshot.docs.map((QueryDocumentSnapshot<Object?> doc) {
        return User.fromJson(doc.data()! as Map<String, dynamic>);
      }).toList();
    }).expand((List<User> users) => users);
    yield* users;
  }
Stereo
  • 1,148
  • 13
  • 36