简体   繁体   English

Flutter Firestore 尝试使用 Stream 我是如何使用 QuerySnapshot

[英]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(),如代码段所示),很想摆脱这个错误,但到目前为止我还没有弄清楚。

如何转换 Stream <querysnapshot<map<string, dynamic> &gt;&gt; 到列表<object> ? Flutter<div id="text_translate"><p> 在 cloud_firestore 的最后一次更新中,当我使用旧代码运行应用程序时出现错误。 如何将 Stream&lt;QuerySnapshot&lt;Map&lt;String, dynamic&gt;&gt;&gt; 转换为列表?</p><p> 我有这个代码,我得到 null 值:</p><pre> Stream&lt;List&lt;Model&gt;&gt; getReviews(String id) { try { return _collectionReference.doc(id).collection('reviews').orderBy('date', descending: true).snapshots().map((reviews) =&gt; reviews.docs.map((review) =&gt; Model.fromJson(review.data()))); } catch (error) { return error.message; }</pre><p> } </p></div></object></querysnapshot<map<string,> - How can I convert a Stream<QuerySnapshot<Map<String, dynamic>>> to a List<Object>? Flutter

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何从 flutter firestore 中的 querysnapshot 获取单个字段值 - How do i get the single field value from querysnapshot in flutter firestore 如何在 Firestore 上中断 querySnapshot? - how to break querySnapshot on Firestore? 如何在列表视图生成器中使用 querySnapshot? (扑) - How to use querySnapshot in a listview builder? (flutter) 如何转换 Stream <querysnapshot<map<string, dynamic> &gt;&gt; 到列表<object> ? Flutter<div id="text_translate"><p> 在 cloud_firestore 的最后一次更新中,当我使用旧代码运行应用程序时出现错误。 如何将 Stream&lt;QuerySnapshot&lt;Map&lt;String, dynamic&gt;&gt;&gt; 转换为列表?</p><p> 我有这个代码,我得到 null 值:</p><pre> Stream&lt;List&lt;Model&gt;&gt; getReviews(String id) { try { return _collectionReference.doc(id).collection('reviews').orderBy('date', descending: true).snapshots().map((reviews) =&gt; reviews.docs.map((review) =&gt; Model.fromJson(review.data()))); } catch (error) { return error.message; }</pre><p> } </p></div></object></querysnapshot<map<string,> - How can I convert a Stream<QuerySnapshot<Map<String, dynamic>>> to a List<Object>? Flutter 如何使用 Flutter 为 FireBase / Firestore 的 1 个文档请求初始化 QuerySnapshot 类型变量? - How to initialize a QuerySnapshot type variable for a 1 document request to FireBase / Firestore with Flutter? 如何从 firestore 查询快照中获取特定的文档数据? - How can I get specific document data from firestore querysnapshot? flutter 中的问题与 QuerySnapshot 中的 fireStore 总是 null - problem in flutter with fireStore in QuerySnapshot its always null 为什么我在 QuerySnapShot firestore flutter 上遇到错误? - Why im getting error on QuerySnapShot firestore flutter? 如何在 Firestore 中使用 Flutter(网络应用)流构建器 - How to use Flutter (web app) stream builder with Firestore 使用 Stream 检索数据<querysnapshot>在 Firebase 和 Flutter</querysnapshot> - Retrieving Data with Stream<QuerySnapshot> in Firebase with Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM