简体   繁体   English

使用 flutter 从 firebase Firestore 获取数据

[英]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 将其推送到数据库中,代码正在运行,但我不明白为什么它会不断生成多个文档,而另一方面使用相同的插件,我们应该使用仅供参考,有大佬能帮帮我吗,谢谢

  • Picture showing pushing data into firestore using geoflutterfire plugin ( but why it keeps generating new documents, i just want to solely update the same document )图片显示使用 geoflutterfire 插件将数据推送到 firestore(但为什么它不断生成新文档,我只想更新同一个文档)

在此处输入图像描述

  • Push data into firebase firestore将数据推送到 firebase 火库
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);
   }
 }
  • get data from firebase firebase从 firebase firebase 获取数据

   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
                ));
       }
    });
  }

  • This is how i'm trying to listen to the data returned from firestore这就是我试图收听从firestore返回的数据的方式
_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.

相关问题 Flutter Firebase - 无法从 Firestore 获取数据 - Flutter Firebase - Not able to fetch data from Firestore 从firestore(firebase)获取数据时出现错误。 我正在使用本机反应 - I am getting an error while fetching data from firestore(firebase). I am using react native 如何使用 excel 或 google 电子表格或 CSV 使用 excel 导入 firebase firestore 数据库中的批量数据 - How to import bulk data in firebase firestore database from excel or google spreadsheet or CSV using flutter React Native Firebase Firestore 数据未正确获取 - React Native Firebase Firestore data not fetching properly 从 flutter firestore 中的子集合中获取特定文档 - fetching a specific document from a subcollection in flutter firestore Firebase Firestore 使用 ID 引用获取数据然后获取引用 - Firebase Firestore fetching data with an ID reference then fetching the reference Flutter 错误 - 断言失败:第 213 行 pos 15:'data:= null':在从 firestore 获取数据时不正确 - Flutter error - failed assertion: line 213 pos 15: 'data != null': is not true at the time of fetching data from firestore Flutter - 无法将数据保存到 Firebase Firestore - Flutter - Unable to save data to Firebase Firestore React Native Firebase Firestore 数据获取与空格 - React Native Firebase Firestore data fetching with empty spaces 从 Firestore Flutter 保存数据 - Save data from Firestore Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM