Indigo
Indigo

Reputation: 177

Having issues saving my user data in Firebase storage during account signup using Flutter

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 "".

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);
      }
    }
  }

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

Here's a screenshot of what I'm seeing on Firebase

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

Upvotes: 0

Views: 136

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80904

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() will update the user, and add a displayName. If you want to add any other extra information, then you have to use the database.

Upvotes: 1

Related Questions