firebase - 在Cloud Firestore中更新用户数据

标签 firebase flutter dart google-cloud-firestore

我在尝试从已登录用户更新数据时遇到问题。我有uid,但uid和用户集合之间必须有连接,以便程序选择合适的用户进行更新,但我不知道该如何进行。

这是我所拥有的:

FirebaseUser loggedInUser;
final _firestore = Firestore.instance;
//
double _latitude;
double _longitude;

void getCurrentLocation() async {
  try {
    Position position = await Geolocator()
      .getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
    setState(() {
      _latitude = position.latitude;
      _longitude = position.longitude;
    });
    _firestore
      .collection('users')
      .document('${loggedInUser.uid}')
      .updateData({'location': GeoPoint(_latitude, _longitude)});
  } catch (e) {
      print(e);
  }
}

这是我得到的:
E/flutter ( 9187): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: PlatformException(Error performing updateData, NOT_FOUND: No document to update: projects/app-#####/databases/(default)/documents/users/CGyByl58ELc0zirlVjJpv5OWAc42, null)

因此它使用的是正确的uid(“CGyByl58ELc0zirlVjJpv5OWAc42”)

这是Firebase的身份验证标签的屏幕截图:
enter image description here

但是我想要获取的是该用户在数据库中的集合的名称:
enter image description here

最佳答案

用户ID与文档ID不同,这就是为什么出现该错误的原因,因为userId不存在任何文档。您需要使用userId作为文档ID:

void addUser() async{
  var firebaseUser = await FirebaseAuth.instance.currentUser();
  Firestore.instance.collection("users").document(firebaseUser.uid).setData(
  {
    "age" : 38,
  }).then((_){
    print("success!");
  });
}

现在,您将使用userId作为文档ID,并且可以使用userId更新文档

关于firebase - 在Cloud Firestore中更新用户数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62244722/

相关文章:

dart - 变量的多个注释 Dart Lang

angular - 如何查询包含一个字段值的对象数组firebase angular?

ios - Firebase如何快速实现具有多个图像的发布功能

java - Android 上的 Android 应用程序未安装错误

ios - iOS 上的 assertUnfrozen

flutter - 使用onWillPop时,后退按钮将被禁用,并且它在应用程序的第一版中发生

dart - Flutter - 堆栈中的定位小部件导致异常

flutter - 在URL中使用URL启动器发送电子邮件时出现问题

flutter - 如何在页面 View 中嵌套 ListView

flutter - 如何检测flutter网站是否在浏览器后台运行?