我正在使用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/