Home > OS >  error: The argument type 'User?' can't be assigned to the parameter type 'User&#
error: The argument type 'User?' can't be assigned to the parameter type 'User&#

Time:01-29

how can I add user data to provider, here my code but get error

  final User? user = (await _auth.signInWithCredential(credential)).user;
  userProvider.addUserData(
    currentUser: user,
    userEmail: user?.email,
    userImage: user?.photoURL,
    userName: user?.displayName,
     );

CodePudding user response:

just replace currentUser: user, with currentUser: user!,

CodePudding user response:

Your User is nullable. This means that User? and User are two different types. Try using a null check (!) as user!. Or you could manually check if the user is null and act differently based on the response.

final User? user = (await _auth.signInWithCredential(credential)).user;
if (user!=null) {
  newUser = user as User;
 userProvider.addUserData(
    currentUser: newUser,
    userEmail: newUser.email,
    userImage: newUser.photoURL,
    userName: newUser.displayName,
     );
} else {
  //Other stuff
}

or

 final User? user = (await _auth.signInWithCredential(credential)).user;
if (user!=null) {
  User newUser = (email: user.email, photoURL: user.photoURL, displayName: user.displayName)
 userProvider.addUserData(
    currentUser: newUser,
    userEmail: newUser.email,
    userImage: newUser.photoURL,
    userName: newUser.displayName,
     );
} else {
  //Other stuff
}
 
  •  Tags:  
  • Related