firebase - 如何在flutter [firebase]中获取文档快照?

标签 firebase flutter dart google-cloud-firestore

我正在使用GeoFlutterFire,这是一个用于查询附近用户位置的软件包。
这是我的代码。

Future<Stream<List<DocumentSnapshot>>> getUsers() async { 
    GeoFirePoint center = geo.point(latitude: lat, longitude: long);
    var collectionref =  Firestore.instance.collection("locations");
    double radius = 50;
    String field = 'position';
    
    Stream<List<DocumentSnapshot>> stream = geo.collection(collectionRef: collectionref).within(center: center, radius: radius, field: field);
    stream.forEach((element) {print(element);});
}

我将其打印在控制台中
[Instance of 'DocumentSnapshot', Instance of 'DocumentSnapshot', Instance of 'DocumentSnapshot', Instance of 'DocumentSnapshot', Instance of 'DocumentSnapshot', Instance of 'DocumentSnapshot', Instance of 'DocumentSnapshot', Instance of 'DocumentSnapshot', Instance of 'DocumentSnapshot', Instance of 'DocumentSnapshot', Instance of 'DocumentSnapshot'] 
我尝试做所有事情,没有任何结果。
我也尝试过从函数返回Stream,但是没有成功。
请帮忙。

最佳答案

请尝试以下操作:

  Stream<List<DocumentSnapshot>> getUsers() async* {
    GeoFirePoint center = geo.point(latitude: lat, longitude: long);
    var collectionref = Firestore.instance.collection("locations");
    double radius = 50;
    String field = 'position';
    yield* geo
        .collection(collectionRef: collectionref)
        .within(center: center, radius: radius, field: field);
  }
使用async*可以返回Stream,然后可以使用yield*返回值,如果要在build()方法中使用此函数,则可以使用StreamBuilder小部件。

关于firebase - 如何在flutter [firebase]中获取文档快照?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63087399/

相关文章:

android - 无法将 JSON 对象插入 Firebase 实时数据库

flutter - 如何在flutter中制作可滚动的屏幕?

class - Dart 确保类中的可选列表参数不为空

dart - 错误 : The argument type 'String?' can't be assigned to the parameter type 'String' because 'String?' is nullable and 'String' isn't

Angular2 Dart - 获取组件的子元素

javascript - Firebase 类型错误的云函数 - 无法读取属性

javascript - 访问节点 firebase 并检查值

flutter - 使用Flutter TDD返回基于json文件的有效BusinessCard模型

dart - 错误chrome_views_delegate.cc(185)-未实现

swift - runTransactionBlock currentData 为空