The type implied by the yield expression must be assingable to

Viewed 1643
yield* FirebaseFirestore.instance

Is giving

The type 'Future<QuerySnapshot>' implied by the 'yield' expression must be assignable to 'Stream<QuerySnapshot>'

The class

  class SearchService {
    Stream<QuerySnapshot> searchByName(
      String searchField, BuildContext context) async* {
     final uid = await TheProvider.of(context).auth.getCurrentUID();
      yield* FirebaseFirestore.instance
         .collection("userData")
        .doc(uid)
        .collection("Contacts")
        .where('searchKey',
            isEqualTo: searchField.substring(0, 1).toUpperCase())
        .get();
     }
    }

I'm quite sure why, does anyone know how one would go about rectifying this?

2 Answers

get() returns a Future<QuerySnapshot> and since you created a function that returns a Stream<QuerySnapshot> then you get that error. You have to use snapshots() instead of get():

yield* FirebaseFirestore.instance
         .collection("userData")
        .doc(uid)
        .collection("Contacts")
        .where('searchKey',
            isEqualTo: searchField.substring(0, 1).toUpperCase())
        .snapshots();

You can create a variable of that type for example of type bool and then yield that, this method will extract the variable from dynamic to the type you want.

Related