[英]fetching data from firebase firestore using flutter
I have an application where i need to get user real time location and push it into database using geoflutterfire, the code is working but i do not understand why it keeps generating multiple documents, while on the other side using the same plugin, we should use only collection reference, can any help me with this, Thank you我有一个应用程序,我需要获取用户实时位置并使用 geoflutterfire 将其推送到数据库中,代码正在运行,但我不明白为什么它会不断生成多个文档,而另一方面使用相同的插件,我们应该使用仅供参考,有大佬能帮帮我吗,谢谢
void updateDriverLocation(){
Fluttertoast.showToast(msg: "Called");
geoFlutterFire = Geoflutterfire();
if(latitude != 0.0 && longitude != 0.0){
GeoFirePoint geoFirePoint = geoFlutterFire.point(latitude: latitude, longitude: longitude);
var map = HashMap<String,Object>();
map['name'] = _firebaseAuth.currentUser!.uid;
map['position'] = geoFirePoint.data;
_firebaseFirestore.collection("Drivers").add(map);
}
}
void getDrivers(){
geoFlutterFire = Geoflutterfire();
GeoFirePoint geoFirePoint = GeoFirePoint(latitude, longitude);
var ref = _firebaseFirestore.collection('Drivers');
var locations = geoFlutterFire.collection(collectionRef: ref).within(center: geoFirePoint, radius: maxRadius, field: 'position');
locations.listen((List<DocumentSnapshot> documentList) {
for (var documentSnapshot in documentList) {
Map<String,Object> map = documentSnapshot.get('geopoint');
double lat = map['latitude'] as double;
double lon = map['longitude'] as double;
//_list.add(LatLng(lat, lon));
markers.add(Marker(
markerId: MarkerId(DateTime.now().millisecond.toString()),
position: LatLng(lat,lon),
icon: BitmapDescriptor.defaultMarker
));
}
});
}
_firebaseFirestore.collection("Drivers").add(map);
The .add()
method creates a new document with a random ID every time. .add()
方法每次都会创建一个带有随机 ID 的新文档。 If you want to update a specific document then you must use update()
on a DocumentReference
.如果要更新特定文档,则必须在DocumentReference
上使用update()
。 Try the following:尝试以下操作:
_firebaseFirestore.collection("Drivers").doc(_firebaseAuth.currentUser!.uid).update(map);
This will update current drivers document only.这将仅更新当前的驱动程序文档。
GeoFlutterFire has the following to specify DocID: GeoFlutterFire 具有以下指定 DocID:
geoRef.setDoc(String id, var data, {bool merge})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.