Reputation: 121
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;
}
Upvotes: 0
Views: 1857
Reputation: 179
its not elegant but you can also do forEach with async pattern as follows
List<documentSnapshot> _myDocs = [];
await stream.forEach((List<documentSnapshot> documentList) {
// do something
documentList.forEach((doc) {
_myDocs.add(doc);
});
})
.timeout(Duration(seconds: 10, // timeout after 10 seconds for example
),
onTimeout () {
// do something on timeout
})
.whenComplete((){
// finally do something
notifyListeners();
});
PS. this is working for me to retrieve and store the streamed document snapshots into a list, but I am still getting an error with " A 'ChangeNotifier' was used after being disposed" (the above snippet is part of a function in a ChangeNotifier class and I call notifyListeners() at the end of the function as shown in the snippet), would love to get rid of this error but so far I haven't figured it out.
Upvotes: 0
Reputation: 235
Continuing the documentation it states you need to listen to the stream. Here is the example they give:
stream.listen((List<DocumentSnapshot> documentList) {
// doSomething()
});
Upvotes: 2