stream.listen(((List< DocumentSnapshot> documentList)返回DocumentSnapshot.Flutter的实例[Firebase] [英] stream.listen((List<DocumentSnapshot> documentList) returns Instance of DocumentSnapshot.Flutter [Firebase]

查看:70
本文介绍了stream.listen(((List< DocumentSnapshot> documentList)返回DocumentSnapshot.Flutter的实例[Firebase]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习Flutter和Dart.

I just started learning Flutter and Dart.

如本文档所述,当我尝试查询附近文档的位置时.[查询] https://pub.dev/packages/geoflutterfire ,这是在控制台"DocumentSnapshot实例"中打印出来的

When I tried to query the locations of nearby documents as stated in this documentation.[To QUERY] https://pub.dev/packages/geoflutterfire , I got this printed in my console "Instance of 'DocumentSnapshot' "

这是我的代码.

Future<void> GetusersLocation() async {
    Position position = await Geolocator()
        .getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
    lat = position.latitude;
    long = position.longitude;
  }

Future<dynamic> getnearbyusers()async{

    await GetusersLocation();

    GeoFirePoint center = geo.point(latitude: lat, longitude: long);

    var collectionReference = _firestore.collection('locations');

    double radius = 50;
    String field = 'position';

 stream = geo.collection(collectionRef: collectionReference)
        .within(center: center, radius: radius, field: field);
  }

在我的脚手架中,我有:

In my Scaffold, I have:

FloatingActionButton(
    onPressed: () async  {
    await getnearbyusers().whenComplete(() => stream.listen((List<DocumentSnapshot> documentList) {
         
         print(documentList[0]);

       }));
      },

推荐答案

您必须首先将DocumentSnapshot转换为Dart Model类,例如

You have to first convert your DocumentSnapshot to a Dart Model class like

例如:

class User {
  final String id;
  final String username;
  final String email;

  User.fromDocumet(DocumentSnapshot documentSnapshot) {
    return User(
        id: documentSnapshot['id'],
        username: documentSnapshot['username'],
        email: documentSnapshot['email']);
  }

  @override
  String toString() {
    return 'id: $id, name: $username, email: $email';
  }
}

然后,您可以使用User模型类中的toString()方法来打印您的值.

Then you can use the toString() method from the User model class to print your value.

上面的User类只是一个例子.请根据文档的结构创建自己的类.

The above User class is just an example. Please create your own class based on how your Document is structured.

这篇关于stream.listen(((List&lt; DocumentSnapshot&gt; documentList)返回DocumentSnapshot.Flutter的实例[Firebase]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆