【问题标题】:User is authenticated but their data is not saved in Cloud Firestore用户已通过身份验证,但其数据未保存在 Cloud Firestore 中
【发布时间】:2021-06-05 11:23:45
【问题描述】:

我对 Flutter 和 Firebase 还很陌生,我一直在关注 Reed Barger 的“使用 Flutter 和 Firebase 构建社交网络”https://www.udemy.com/course/build-a-social-network-with-flutter-and-firebase 的指南。我已成功通过 Google 完成身份验证,但在 Firestore 中创建用户时遇到问题。方法是

createUserInFirestore() async {
    //1) check if user exists in user's collection in database (according to their id)
    final GoogleSignInAccount user = googleSignIn.currentUser;
    final DocumentSnapshot docu = await usersRef.doc(user.id).get();
    //2) If user doesn't exist, take them to create account page
    if (!docu.exists) {
      final username = await Navigator.push(
          context, MaterialPageRoute(builder: (context) => CreateAccount()));

      //3)  get user name from create account and use it to create new user's document in user's collection
      usersRef.doc(user.id).set({
        "id": user.id,
        "username": username,
        "photoUrl": user.photoUrl,
        "email": user.email,
        "displayName": user.displayName,
        "bio": "",
        "timestamp": timestamp
      });
    }
  }

问题在于它不会将新用户带到 CreateAccount 页面,并且 Firestore 数据库也没有更新。相反,主页已加载,我收到错误

E/flutter (17311): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: NoSuchMethodError: The method 'doc' was called on null.
E/flutter (17311): Receiver: null
E/flutter (17311): Tried calling: doc("1148131790923706651234")

此外,用户不会添加到 Firebase 控制台中的 Users 下的 Authentication 中。目前,我只在 Android 上工作。 我哪里出错了?

【问题讨论】:

    标签: flutter google-cloud-firestore


    【解决方案1】:

    什么是 usersRef?你还没有定义它,所以它是空的。您正在对空变量 usersRef 调用方法“doc”。尝试这样的方法来初始化 usersRef:

    CollectionReference usersRef = FirebaseFirestore.instance.collection('Users');
    

    【讨论】:

    • 我在页面顶部定义了它。我实际上发现问题实际上是 Firebase 没有初始化。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-28
    • 1970-01-01
    • 2016-12-31
    • 2017-08-07
    • 1970-01-01
    • 2015-02-24
    • 2016-06-01
    相关资源
    最近更新 更多