P R R S
P R R S

Reputation: 39

Firebase Email Validation Flutter

I have this piece of code to sign up:

      Future<String> signUp({String email, String password}) async {
    try{
      await _firebaseAuth.createUserWithEmailAndPassword(email: email, password: password);

      return "Signed up";
    } on FirebaseAuthException catch(e){
      print("no se puede crear");
      return e.message;
    }
  }

I need to implement the validation mail from firebase, this has been asked a lot and I came up with this when researching, some code is deprecated and its hard for me to find guidance:

    Future<String> signUp({String email, String password}) async {
    FirebaseUser user = await _firebaseAuth.createUserWithEmailAndPassword(email: email, password: password);
    try {
      await user.sendEmailVerification();
      return user.uid;
    } catch (e) {
      print("An error occured while trying to send email        verification");
      print(e.message);
    }
  }
  }

But cant figure how to implement it because it throws:

A value of type 'UserCredential' can't be assigned to a variable of type 'FirebaseUser'.

Upvotes: 0

Views: 436

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317372

createUserWithEmailAndPassword doesn't return a FirebaseUser object. As you can see from the linked API documentation, it yields a UserCrediental object. That's what the error message is trying to tell you - you can't assign a UserCredential to a FirebaseUser typed variable.

If you wan to get a FirebaseUser object out of a UserCredential, you can simply use its user property.

UserCredential credential = await _firebaseAuth.createUserWithEmailAndPassword(email: email, password: password);
FirebaseUser user = credential.user;

Upvotes: 1

Related Questions