skip to Main Content

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

3

Answers


  1. Chosen as BEST ANSWER

    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
                        
    

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

    Login or Signup to reply.
  3. 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;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search