firebase - 使用Flutter进行帐户注册时将我的用户数据保存到Firebase存储中时出现问题

标签 firebase flutter dart google-cloud-firestore firebase-authentication

因此,在注册时,用户将输入其名称,用户名以及电子邮件和密码。提交后,只有用户的电子邮件和ID会传递到Firebase,但profileName为“”,而username为null。我只期望username和profileName的值,其他所有值仍应为“”。
这是从SignUpPage.dart
我不确定是否通过添加用户名:user.displayName和profileName:user.displayName正确地完成了LoggedInUser部分。我这样做是因为我看到firebase_auth仅使用displayName。因此,如果我在那里也错了,请纠正我。

Future signUpUser() async {
    if (_formKey.currentState.validate()) {
      _formKey.currentState.save();
      try {
        AuthResult authResult = await auth.createUserWithEmailAndPassword(email: email, password: password);
        FirebaseUser signedInUser = authResult.user;
        if (signedInUser != null) {
          var user = await FirebaseAuth.instance.currentUser();
          //currentUser = User();
          User loggedInUser;
          loggedInUser = User(id: user.uid, username: user.displayName, profileName: user.displayName, email: user.email);
          DocumentSnapshot documentSnapshot = await usersReference.document(loggedInUser.id).get();

          if (!documentSnapshot.exists) {
            usersReference.document(loggedInUser.id).setData({
              "id": loggedInUser.id,
              "profileName": loggedInUser.profileName,
              "username": loggedInUser.username,
              "photoUrl": "",
              "email": loggedInUser.email,
              "bio": "",
              "timestamp": timestamp,
              "talkingTo": null,
            });
            await followersReference
                .document(loggedInUser.id)
                .collection("userFollowers")
                .document(loggedInUser.id)
                .setData({});
            documentSnapshot = await usersReference.document(loggedInUser.id).get();
          }
          loggedInUser = User.fromDocument(documentSnapshot);
        }
        Navigator.push(
            context, MaterialPageRoute(builder: (context) => HomePage()));
      } catch (e) {
        print(e);
      }
    }
  }
这是我的user.dart文件中的内容(如果有帮助的话)
class User {
  final String id;
  final String profileName;
  final String username;
  final String photoUrl;
  final String url;
  final String email;
  final String bio;
  final String createdAt;

  User({
    this.id,
    this.profileName,
    this.username,
    this.photoUrl,
    this.url,
    this.email,
    this.bio,
    this.createdAt,
  });

  factory User.fromDocument(DocumentSnapshot doc) {
    return User(
      id: doc.documentID,
      email: doc['email'],
      username: doc['username'],
      photoUrl: doc['photoUrl'],
      url: doc['url'],
      profileName: doc['profileName'],
      bio: doc['bio'],
      createdAt: doc['createdAt'],
    );
  }
}
Here's a screenshot of what I'm seeing on Firebase
让我知道如果我还要在此帖子中添加其他内容,那将会有所帮助。任何帮助表示赞赏。谢谢!

最佳答案

当您使用createUserWithEmailAndPassword创建用户时,该用户可以使用电子邮件登录并进行身份验证。但是用户没有displayName,您需要执行以下操作来添加displayName:

      var user = await FirebaseAuth.instance.currentUser();
      var updateInfo = UserUpdateInfo();
      updateInfo.displayName = "peter";
      await user.updateProfile(updateInfo);

      await user.reload();
      user = await FirebaseAuth.instance.currentUser();

      User loggedInUser;
      loggedInUser = User(id: user.uid, username: user.displayName, profileName: user.displayName, email: user.email);
updateProfile()将更新用户,并添加displayName。如果要添加任何其他信息,则必须使用数据库。

关于firebase - 使用Flutter进行帐户注册时将我的用户数据保存到Firebase存储中时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62922124/

相关文章:

ios - 如何从 Firestore 返回数组中的数据?

使用 Google Chrome 的 "network conditions"选项卡时,Firebase 托管为 Googlebot 用户代理返回 500 内部错误?

visual-studio-code - 使用 Visual Studio Code 设置 Flutter

flutter - 类型 'Null' 不是类型转换中类型 'Map<String, dynamic>' 的子类型

javascript - dart2js 代码如何比 javascript 更快?

listview - Flutter/Dart : Handling Dropdown changes within a ListView. 生成器,下拉按钮未更新,

firebase - 如何使用 api key 向 Firebase 实时数据库发出经过身份验证的请求

swift - 如何在 swift 中从 webView 打开外部链接?

android - 如何在 Flutter 的 TextField 中显示错误图标

dart - Dart:确定img标签的大小(innerWidth)