使用Flutter进行帐户注册时将用户数据保存到Firebase存储中时出现问题 [英] Having issues saving my user data in Firebase storage during account signup using Flutter

查看:107
本文介绍了使用Flutter进行帐户注册时将用户数据保存到Firebase存储中时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,在注册时,用户将输入其名称,用户名以及电子邮件和密码。提交后,只有用户的电子邮件和ID会传递到Firebase,但profileName是。并且用户名为null。我只是期望用户名和profileName的值,其他所有值都应该仍然是。

So on signup, the user will enter their name, username, along with the email and a password. On submit, the user's email and id are the only things that are delivered to Firebase but profileName is "" and username is null. I'm only expecting a value for username and profileName, everything else should still be "".

这是从SignUpPage.dart
来的,我不确定是否可以通过添加用户名:user.displayName和profileName:user.displayName来正确完成了loginInUser部分。我这样做是因为我看到firebase_auth仅使用displayName。因此,如果我也错了,也请纠正我。

This is from SignUpPage.dart I'm not sure if I did that loggedInUser part right by adding username: user.displayName and profileName: user.displayName. I did that because I saw firebase_auth only uses displayName. So correct me if I'm wrong there too.

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文件中的内容,如果

Here's what's in my user.dart file, if it helps

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'],
    );
  }
}

让我知道我是否应该在此帖子中添加其他内容,这将对您有所帮助。任何帮助表示赞赏。谢谢!

Let me know if there's anything else I should add to this post, that would help. Any help is appreciated. Thanks!

推荐答案

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

When you create a user using createUserWithEmailAndPassword, the user then can login using the email and will be authenticated. But the user won't have a displayName, you need to do the following to add the 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 。如果要添加任何其他信息,则必须使用数据库。

updateProfile() will update the user, and add a displayName. If you want to add any other extra information, then you have to use the database.

这篇关于使用Flutter进行帐户注册时将用户数据保存到Firebase存储中时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆