【问题标题】:Firestore returns me null value when I call the get method?当我调用 get 方法时,Firestore 会返回 null 值吗?
【发布时间】:2021-07-23 13:23:16
【问题描述】:

Firestore 为 value.data 返回 null ??我该如何解决?以及如何在没有自定义字符串的情况下传递文档 ID,并将其放在这里。我在 cloud firestore 中的文档 id 和身份验证中的 uid 相同,我可以链接 uid 以从 firestore 获取数据吗? 我是 Flutter 和 Firebase 的初学者。

class _MyHomePageState extends State<MyHomePage> {
  final _auth = FirebaseAuth.instance;

  CollectionReference users = FirebaseFirestore.instance.collection('users');
  User? currentUser;
  dynamic? data;

  void getCurrentUser() async {
    try {
      final user = _auth.currentUser;
      
      await users.doc("QWhAkxSEXDfn4HJTdwtO1MFnWgz2").get().then((value) {
        print(value.data());
      });
      if (user != null) {
        currentUser = user;
        print(currentUser?.email);
      }
    } on FirebaseAuthException catch (e) {
      print('Failed with error code: ${e.code}');
      print(e.message);
    }
  }

  @override
  void initState() {
    // TODO: implement initState
    getCurrentUser();
    super.initState();
  }

有什么与flutter web和firestore有关的吗?但我可以将数据添加到集合中,但在读取时返回 null

【问题讨论】:

    标签: firebase flutter google-cloud-firestore


    【解决方案1】:

    users.doc 之前使用await,像这样:

    await users.doc("QWhAkxSEXDfn4HJTdwtO1MFnWgz2").get().then((value) {
            print(value.data());
          });
    

    并修复您的收藏参考,

    instance.collection('users');```
    
    It should be "User" as the name in your firebase.
    

    【讨论】:

    • 不,它也返回空值
    • 编辑了我的答案,看看吧。
    猜你喜欢
    • 2017-11-26
    • 2016-04-25
    • 2019-03-10
    • 2014-12-09
    • 1970-01-01
    • 1970-01-01
    • 2019-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多