【问题标题】:How to get document snapshot in flutter[firebase]?如何在颤振[firebase]中获取文档快照?
【发布时间】:2020-07-25 10:48:35
【问题描述】:

我正在使用 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,但没有成功。 请帮忙。

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore


    【解决方案1】:

    尝试以下方法:

      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小部件.

    【讨论】:

    • 嘿!兄弟,我正在我的控制台中打印此“Instance of '_ControllerStream>'”你能告诉我,我怎样才能在控制台中打印流?
    • 你怎么称呼它?
    • 嗯..bro 我真的是飞镖新手。当我按下我的应用程序中的按钮时,我想要的只是调用一个函数来在我的控制台中打印附近的用户。我尝试了本文档中提供的方式来查询用户,文档在这里pub.dev/packages/geoflutterfire 我尝试在一个函数中实现它,但是我收到了错误“DocumentSnapshot 的实例。请帮助。
    • 嘿..看看这个..我试过这段代码,但没有成功。 :(stackoverflow.com/questions/63099231/…
    猜你喜欢
    • 2022-01-14
    • 2021-10-30
    • 2019-12-31
    • 2020-07-12
    • 2021-07-12
    • 2020-07-15
    • 1970-01-01
    • 2021-09-07
    • 2019-08-02
    相关资源
    最近更新 更多