sempakonka
sempakonka

Reputation: 35

Firebase createUserWithEmailAndPassword not working as expectedly

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

Upvotes: 0

Views: 1456

Answers (3)

Bilal Almefleh
Bilal Almefleh

Reputation: 376

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

Upvotes: 1

sempakonka
sempakonka

Reputation: 35

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
                    

Upvotes: 0

Thanh Nguyen Chi Song
Thanh Nguyen Chi Song

Reputation: 16

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

Upvotes: 0

Related Questions