Home > Back-end >  Firebase createUserWithEmailAndPassword not working expectedly
Firebase createUserWithEmailAndPassword not working expectedly

Time:08-15

I am calling createUserWithEmailAndPassword. When succesful, then callback should be running. However, it is not. whenComplete callback is running as expected. There is no error, so onError is not running, as expected. It is a problem because I need the parameter in the then.

Why is it doing this?

I am using Flutter Web

 FirebaseAuth auth = FirebaseAuth.instance;
                      await auth
                          .createUserWithEmailAndPassword(
                              email: emailController.text,
                              password: passwordController.text)
                          .then((value) => () async {
                                print("user created");
                           
                                return value;
                              })
                          .whenComplete(() {
                        print("when callback");
                      }).onError((error, stackTrace) {
                        print("error: $error");
                        return Future.value();
                      });

CodePudding user response:

try this :

  // For registering a new user
 static Future<User?> registerUsingEmailPassword({
 required String name,
 required String email,
required String password,
 }) async {
FirebaseAuth auth = FirebaseAuth.instance;
User? user;

try {
  UserCredential userCredential = await 
 `enter code here`auth.createUserWithEmailAndPassword(
     email: email.trim(),
    password: password.trim(),
  );

  user = userCredential.user;
  await user!.updateDisplayName(name);
  await user.reload();
  user = auth.currentUser;
} on FirebaseAuthException catch (e) {
  if (e.code == 'weak-password') {
    print('The password provided is too weak.');
  } else if (e.code == 'email-already-in-use') {
    print('The account already exists for that email.');
  }
} catch (e) {
  print(e);
}

return user;
}

CodePudding user response:

Please test on it to make sure process - the theory - it go to whenComplete before go to then please - it is process.

CodePudding user response:

I got it working.

FirebaseAuth _firebaseAuth = FirebaseAuth.instance;

                      UserCredential uc =
                          await _firebaseAuth.createUserWithEmailAndPassword(
                              email: emailController.text,
                              password: passwordController.text)
                      .onError((error, stackTrace)  
                          {
                            // error callback
                          });
                      print(uc.user!.uid);
                      print("user created");
                      // do stuff with uc.user.uid
                    
  • Related