[英]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.我是 flutter 和 firebase 的新手,我正在尝试将 Firestore 文档添加到列表中。 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.我现在正在使用 Flutter 包:“GeoFlutterFire”,以便我可以通过地理位置查询 Firestore 文档。
I have been following the readme from: https://pub.dev/packages/geoflutterfire我一直在关注自述文件: 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?我的问题是,有什么方法可以像我在第一个代码示例中所做的那样,将该流中的文档添加到我的
_listingList
中?
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我已经尝试使用提供的 Stream 示例切换出 QuerySnapshot 并使用流更改快照,但是
stream.documents.forEach((document)
不起作用
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;
}
Continuing the documentation it states you need to listen to the stream.继续文档,它指出您需要收听流。 Here is the example they give:
这是他们给出的例子:
stream.listen((List<DocumentSnapshot> documentList) {
// doSomething()
});
its not elegant but you can also do forEach with async pattern as follows它并不优雅,但您也可以使用异步模式执行 forEach 如下
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.
这对我来说可以检索流文档快照并将其存储到列表中,但我仍然收到错误消息“在处理后使用了‘ChangeNotifier’”(上面的代码片段是 ChangeNotifier 类中的一个函数的一部分,并且我在函数的末尾调用了 notifyListeners(),如代码段所示),很想摆脱这个错误,但到目前为止我还没有弄清楚。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.