Search code examples
firebasefluttergoogle-cloud-firestoregeolocation

Flutter Firestore trying to use Stream how I was using QuerySnapshot


I am new to flutter and firebase and I am trying to add Firestore documents to a list. I have been successful in doing this using this code:

getLocationListings(ListingNotifier listingNotifier, String location) async {
  QuerySnapshot snapshot = await Firestore.instance
      .collection('listings')
      .where('location', isEqualTo: location)
      .getDocuments();
      print(location);

  List<Listing> _listingList = [];

  snapshot.documents.forEach((document) {
    Listing listing = Listing.fromMap(document.data);
    _listingList.add(listing);
  });

  listingNotifier.listingList = _listingList;
}

I am now using the Flutter package: "GeoFlutterFire" so that I can query Firestore documents by geolocation.

I have been following the readme from: https://pub.dev/packages/geoflutterfire and to read data it looks like he is using a Stream like this:

Stream<List<DocumentSnapshot>> stream = geo.collection(collectionRef: collectionReference)
                                        .within(center: center, radius: radius, field: field);

My question is, is there any way I can add the documents from that stream into my _listingList like I am doing in my first code example?

I have tried just switching out the QuerySnapshot with the provided Stream example and changing snapshot with stream, but stream.documents.forEach((document) doesn't work

Geoflutterfire geo = Geoflutterfire();
Firestore _firestore = Firestore.instance;
GeoFirePoint center = geo.point(latitude: lat, longitude: lng);
var collectionReference = _firestore.collection('listings');
double radius = 50;
String field = 'position';

getLocationListings(ListingNotifier listingNotifier, GeoFirePoint location) async {

  Stream<List<DocumentSnapshot>> stream = geo.collection(collectionRef: collectionReference)
                                        .within(center: center, radius: radius, field: field);


  List<Listing> _listingList = [];

  stream.documents.forEach((document) {
    Listing listing = Listing.fromMap(document.data);
    _listingList.add(listing);
  });

  listingNotifier.listingList = _listingList;
}

Solution

  • Continuing the documentation it states you need to listen to the stream. Here is the example they give:

        stream.listen((List<DocumentSnapshot> documentList) {
          // doSomething()
        });