Home > Mobile >  type '() => Map<String, dynamic>?' is not a subtype of type 'Map<String,
type '() => Map<String, dynamic>?' is not a subtype of type 'Map<String,

Time:05-26

can I have some help regarding this error? Here is the attachment of the code and error issue

enter image description here

CodePudding user response:

Map<String, dynamic>? is nullable, while Map<String, dynamic> is not nullable, therefore the former is not a subtype of the latter and the error message is expected. You will need to make sure that either both are nullable or none of them are nullable.

CodePudding user response:

In your code snapshot.data.data is actually a method thata you can call to get the data, that means, snapshot.data.data() gives you Map<String, dynamic>?, since it is possible to have a DocumentSnapshot that doesn't exist and thus has no (data) fields.

This leads to having two types in play here:

  • Map<String, dynamic> is a map of string keys and values of an unknown type.
  • Map<String, dynamic>? can either be a map or be null, in your snapshot.data.data.

You can't assign a value of the second type to a variable of the first type, as such a variable has no way to store the null value.

So you must either make the variable nullable too, by making it of type Map<String, dynamic>?, or (and usually better) you can assert in your code that the value is not null before assigning it to the variable.

The latter would look something like:

if (snapshot.data.data != null) {
  userProfile.setData(snapshot.data.data()!);
}
  • Related