3

I'm new to dart and flutter and I got this error:

error: The argument type 'String?' can't be assigned to the parameter type 'String'.

This error occurred when I tried to get fields of a user object from firebase and pass it to the User constructer. I tried the answer from another post, which is to do a null check on the returned user object and uid, but even inside the if statement dart still gives me this error.

Here is the function that the error occurred:

Future<UserSchema> signInWithEmailAndPassword(
      String email, String password) async {
    var authResult = await FirebaseAuth.instance
        .signInWithEmailAndPassword(email: email, password: password);
    return UserSchema(authResult.user.uid, authResult.user.email,
        authResult.user.displayName, authResult.user.password);
}

And this is the User class:

class UserSchema {
  String uid;
  String email;
  String displayName;
  String phoneNumber;
  String password;
  String city;
  String province;
  String postCode;
  bool hasTransit;
  String streetAddress;

  UserSchema(this.uid, this.email,this.displayName,this.password,
      {this.phoneNumber = '0000000000',
      this.postCode = '000000',
      this.streetAddress = '000 0000 0000',
      this.city = '00',
      this.province = '00',
      this.hasTransit = false});

  Map<String, dynamic> toJson() => {
  'uid': uid.trim(),
  'displayName': displayName.trim(),
  'email': email.trim(),
  'phoneNumber': phoneNumber.trim(),
  'accountCreated':
    DateFormat('dd/MM/yyyy').format(Timestamp.now().toDate()),
    'shippingAddress': {
      'streetAddress': streetAddress.trim(),
      'city': city.trim(),
      'PostCode': postCode.trim(),
      'province': province.trim(),
    },
    'hasTransit': hasTransit,
  };

I would really appreciate a more detailed answer, thank you.

01D5D
  • 31
  • 1
  • 2
  • "even inside the `if` statement dart still gives me this error." You don't show the code where you do that, but you almost certainly checked a non-local variable. Only *local* variables can be type-promoted. See https://stackoverflow.com/q/65035574/ – jamesdlin Jan 08 '22 at 06:16

1 Answers1

0

It is hard for me to tell you the exact change you have to make because I am not super sure which variable has the error, regardless, I will explain to you what the error means, why it happens and how to properly deal with it.

Why does this happen?

Some variable here is nullable that's to say it could be null, but you are trying to assign it to a non-nullable variable, so you get an error.

I am not sure which variable it is (or if it is more that one) so we will proceed assuming it is authResult.user.email for this example:

How do I fix it?

To fix it, you can use the ! operator. If you place the bang operator besides a nullable value, this error will disappear. But beware that if the value is in fact null, your app will crash. It is a dangerous operator that you should only use when you are certain the value will not be null.

authResult.user.email!

Another way to fix it is to provide some default value in case the original value is null, you can do this by using the ?? operator:

authResult.user.email ?? 'example@email.com';

Just to be absolutely clear. I don't think you should give a default email value, it doesn't make sense for an email to have a default value. Also I believe authResult.user.email will only be null if the user uses anonymous sign In, so you should not have any problem with using !.

h8moss
  • 4,626
  • 2
  • 9
  • 25