Firebase/Futter user.displayName 字段在存储在 cloud-firestore 时存储为 null

问题描述 投票:0回答:1

我正在使用 Flutter 和 Firebase 构建一个应用程序。我正在尝试在首次登录应用程序时创建用户个人资料。我正在使用 Firebase 身份验证提供的 Google 登录方法。

这是我的代码:

void _handleGoogleSignIn() async {
    try {
      GoogleAuthProvider googleAuthProvider = GoogleAuthProvider();
      UserCredential userCredential =
          await _auth.signInWithProvider(googleAuthProvider);
      User? user = userCredential.user;

      // check if user pf exists
      if (user != null) {
        DocumentSnapshot userDoc = await FirebaseFirestore.instance
            .collection("users")
            .doc(user.uid)
            .get();

        // if not there, then create
        if (!userDoc.exists) {
          await FirebaseFirestore.instance
              .collection("users")
              .doc(user.uid)
              .set(
            {
              "photoUrl": user.photoURL,
              "displayName": user.displayName,
              "email": user.email,
            },
          );
        }
      }
    } catch (e) {
      showDialog(
        context: context,
        builder: (context) {
          return AlertDialog(
            content: Text(e.toString()),
          );
        },
      );
    }
  }

我正在存储displayName、photoUrl、email 和其他字段(仍在思考)。但在 Firestore 窗口上,它显示该文档是在将 displayName 设置为 null 的情况下存储的

那么我做错了什么,解决办法是什么?

flutter firebase authentication google-cloud-firestore firebase-authentication
1个回答
0
投票

创建用户时显示名称将为空,创建用户后需要更新

_auth.currentUser.updateDisplayName('Insert the name here');
© www.soinside.com 2019 - 2024. All rights reserved.