Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix error: A value of type 'User?' can't be assigned to a variable of type 'User'

I am a beginner in app development and am having trouble with this error. The error I get is "error: A value of type 'User?' can't be assigned to a variable of type 'User'."

My Code:

Future signUpWithEmailAndPassword( String email, String password) async {
    try {
      UserCredential result = await _auth.createUserWithEmailAndPassword(email: email, password: password);
      User user = result.user;
      return _userFromFirebaseUser(user);
    } catch(e){
      print(e.toString());
      return null;
    }
  }
like image 654
VOnStack Avatar asked Dec 23 '22 15:12

VOnStack


2 Answers

change the line

User user = result.user;

to

User? user = result.user;
like image 179
Hemal Moradiya Avatar answered Dec 25 '22 05:12

Hemal Moradiya


You can actually fix that error by using !. But make sure that result.user is not null!

User user = result.user!;
like image 44
quoci Avatar answered Dec 25 '22 05:12

quoci