I have 2 methods. One that creates user called signUp
and other that adds display name to user profile called updatesUser
. During the signUp process, I am calling updateUser method as well.
Once registration is successful, the user is sent to HomeScreen.
However, on signUp completion when user is at HomeScreen, the display name is shown as "null". But then it works when the same user logs out and comes back in.
User object and refs are defined in parent class of these methods:
class FirebaseController extends GetxController {
FirebaseAuth _auth = FirebaseAuth.instance;
Rx<User> _firebaseUser = Rx<User>();
String get user => _firebaseUser.value?.email;
String get userid => _firebaseUser.value?.uid;
String get displayName => _firebaseUser.value?.displayName;
void signUp(String username, String email, String password) async {
CollectionReference dbref = FirebaseFirestore.instance.collection("users");
await _auth
.createUserWithEmailAndPassword(email: email, password: password)
.then((value) {
dbref
.doc(userid)
.set({
"username": username,
"email": email,
"password": password,
"uid": userid,
})
.then((value) => updateUser(username: username))
.then((value) => Get.offAll(HomeScreen()));
}).catchError(
(onError) =>
Get.snackbar("Error while creating account ", onError.message),
);
}
This is my updateUser method that is called during "signUp"
void updateUser({username}) async {
await _auth.currentUser
.updateProfile(displayName: username)
.then((value) => _firebaseUser.value.reload())
.then((value) => Get.snackbar("User Created", "User has been created"))
.catchError((onError) =>
Get.snackbar("Error while Updating DisplayName ", onError.message));
}
What am I doing Wrong?!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…