Flutter Firestore 尝试使用 Stream 我使用 QuerySnapshot 的方式
Flutter Firestore trying to use Stream how I was using QuerySnapshot
我是 flutter 和 firebase 的新手,我正在尝试将 Firestore 文档添加到列表中。我已经使用以下代码成功地做到了这一点:
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;
}
我现在正在使用 Flutter 包:"GeoFlutterFire" 这样我就可以通过地理位置查询 Firestore 文档。
我一直在关注来自:https://pub.dev/packages/geoflutterfire 的自述文件并读取数据,看起来他正在使用这样的 Stream:
Stream<List<DocumentSnapshot>> stream = geo.collection(collectionRef: collectionReference)
.within(center: center, radius: radius, field: field);
我的问题是,有什么方法可以像我在第一个代码示例中所做的那样将该流中的文档添加到我的 _listingList
中吗?
我试过使用提供的 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;
}
继续说明您需要收听流的文档。这是他们给出的例子:
stream.listen((List<DocumentSnapshot> documentList) {
// doSomething()
});
它并不优雅,但您也可以使用异步模式执行 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。这对我来说很有效,可以检索流式文档快照并将其存储到列表中,但我仍然遇到错误“ A 'ChangeNotifier' was used after being disposed”(上面的代码片段是 ChangeNotifier 中函数的一部分class 并且我在函数末尾调用了 notifyListeners(),如代码片段所示),很想消除这个错误,但到目前为止我还没有弄清楚。
我是 flutter 和 firebase 的新手,我正在尝试将 Firestore 文档添加到列表中。我已经使用以下代码成功地做到了这一点:
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;
}
我现在正在使用 Flutter 包:"GeoFlutterFire" 这样我就可以通过地理位置查询 Firestore 文档。
我一直在关注来自:https://pub.dev/packages/geoflutterfire 的自述文件并读取数据,看起来他正在使用这样的 Stream:
Stream<List<DocumentSnapshot>> stream = geo.collection(collectionRef: collectionReference)
.within(center: center, radius: radius, field: field);
我的问题是,有什么方法可以像我在第一个代码示例中所做的那样将该流中的文档添加到我的 _listingList
中吗?
我试过使用提供的 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;
}
继续说明您需要收听流的文档。这是他们给出的例子:
stream.listen((List<DocumentSnapshot> documentList) {
// doSomething()
});
它并不优雅,但您也可以使用异步模式执行 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。这对我来说很有效,可以检索流式文档快照并将其存储到列表中,但我仍然遇到错误“ A 'ChangeNotifier' was used after being disposed”(上面的代码片段是 ChangeNotifier 中函数的一部分class 并且我在函数末尾调用了 notifyListeners(),如代码片段所示),很想消除这个错误,但到目前为止我还没有弄清楚。