简体   繁体   中英

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

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

在此处输入图像描述

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.

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()!);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM