Home > Net >  The argument type 'String?' cant be assigned to the parameter type 'String'. I a
The argument type 'String?' cant be assigned to the parameter type 'String'. I a

Time:12-30

UserCredential result= await _firebaseAuth.signInWithCredential(credential);

User? userDetails = result.user;

if(result!= null)
{
    SharedPreferenceHelper().saveUserEmail(userDetails.email);
    SharedPreferenceHelper().saveUserId(userDetails.uid);
    SharedPreferenceHelper().saveDisplayName(userDetails.displayName);
    SharedPreferenceHelper().saveUserProfileUrl(userDetails.photoURL);
}

This was my code, how can I reslove it?

I tried replacing '.' with '?.' and '!.' however it is not working

CodePudding user response:

Try to handle with the null operator and give some default value if they are null.

  UserCredential result= await _firebaseAuth.signInWithCredential(credential);
    
    User? userDetails = result.user;
    
    if(result!= null)
    {
        SharedPreferenceHelper().saveUserEmail(userDetails?.email??'');
        SharedPreferenceHelper().saveUserId(userDetails?.uid??'');
        SharedPreferenceHelper().saveDisplayName(userDetails?.displayName??'');
        SharedPreferenceHelper().saveUserProfileUrl(userDetails?.photoURL??'');
    }

CodePudding user response:

if the value of userDetails.email, userDetails.uid,userDetails.displayName etc is null try this

SharedPreferenceHelper().saveUserEmail(userDetails.email!);
SharedPreferenceHelper().saveUserId(userDetails.uid!);
SharedPreferenceHelper().saveDisplayName(userDetails.displayName!);
SharedPreferenceHelper().saveUserProfileUrl(userDetails.photoURL!);

it will solve your problem if not then let me know

CodePudding user response:

Explanation:

This is happening because you have declared userDetails as User? which means userDetails can be null but saveUserEmail() takes in type of String which cannot be null

Try with

// When you are sure that userDetails can never be null
SharedPreferenceHelper().saveUserEmail(userDetails.email!);

or

// When you want to store '' if the email is null
SharedPreferenceHelper().saveUserEmail(userDetails?.email ?? '');

or

User userDetails; // This option here is not possible because result.user returns type of User?
SharedPreferenceHelper().saveUserEmail(userDetails.email!);

Note: Follow the same steps for userDetails.uid,userDetails.photoUrl,userDetails.displayName

  • Related