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

Viewed 3759

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

change the line

User user = result.user;

to

User? user = result.user;

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

User user = result.user!;

This worked for me:

1. Right-click the line which has the error

2. Click on more options

3. Click on Add cast

Watch this tutorial for connecting Firebase with your Flutter app

Related