skip to Main Content

I have following code which does not work somehow:

Future<void> createUserWithEmailAndPassword() async {
  //try nutzer erstellen
  try {
    UserCredential userCredential =
    await Auth().createUserWithEmailAndPassword(
      email: _controllerEmail.text,
      password: _controllerPassword.text
    );
  } 
  
  catch (e) {
    if (e is FirebaseAuthException) {
      setState(() {
        errorMessage = e.message;
      });
    }
  }
}

I want to add the userCredential variable so I can use it later on but somehow it give me the following error:

This expression has a type of ‘void’ so its value can’t be used.
Try checking to see if you’re using the correct API; there might be a function or call that returns void you didn’t expect. Also check type parameters and variables which might also be void.dartuse_of_void_result

I want to use the userCredential variable so I can create in Cloud Firestore additional information for a user in a document.

Any ideas on how to solve?

2

Answers


  1. The function createUserWithEmailAndPassword returns indeed a Future containing the UserCredential.

    Please make sure that the return type of the function used in the Auth class matches the return type of the function createUserWithEmailAndPassword provided by FirebaseAuth.

    It should look like this:

    class Auth {
      static Future<UserCredential> createUserWithEmailAndPassword({
        required String email,
        required String password,
      }) =>
          FirebaseAuth.instance
              .createUserWithEmailAndPassword(email: email, password: password);
    }
    
    Login or Signup to reply.
  2. instead of using

    Auth()..createUserWithEmailAndPassword(
        email: _controllerEmail.text,
        password: _controllerPassword.text
      );
    

    try using

    FirebaseAuth.instance.createUserWithEmailAndPassword(
        email: _controllerEmail.text,
        password: _controllerPassword.text
      ); 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search