我正在尝试使用 firebase 查询来获取附近的位置,并且进展顺利。这是获取附近位置的函数。
Future<List<DocumentSnapshot>> nearbyLocations() async {
CollectionReference collectionRefer =
FirebaseFirestore.instance.collection('locations');
double radius = 10;
String field = 'position';
List<DocumentSnapshot> docList;
GeoFirePoint center = await getCurrentLocation();
// print(center);
Stream<List<DocumentSnapshot>> stream = geo
.collection(collectionRef: collectionRefer)
.within(center: center, radius: radius, field: field, strictMode: true);
stream.listen((List<DocumentSnapshot> documentList) {
if (documentList.length > 0) {
print(documentList[0].data());
docList = documentList;
} else {
return {};
}
});
}
我知道查询将只返回一个数据。所以,我在上面的函数中打印了第一个值。返回 documentList 时会出现问题。
loadData() async {
documentList =
await GeoLocator().nearbyLocations();
}
当我调用上面的函数时,它打印空。但是当我试图在nearbyLocations()它打印数据时打印。但不是当我打电话时loadData()。我将在列表视图中使用这个返回的数据。