我刚刚开始学习Flutter和Dart。
如本文档所述,当我尝试查询附近文档的位置时。[至查询] https://pub.dev/packages/geoflutterfire,我在控制台“ 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);
}
在我的脚手架中,我有:
FloatingActionButton(
onPressed: () async {
await getnearbyusers().whenComplete(() => stream.listen((List<DocumentSnapshot> documentList) {
print(documentList[0]);
}));
},
答案 0 :(得分:1)
首先必须将DocumentSnapshot转换为Dart Model类,例如
例如:
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()方法来打印您的值。
上面的User类只是一个例子。请根据文档的结构创建自己的类。