问题描述
{“数据”:[{“ id”:“ 32f”,“ regionName”:“韩国”,“ companyName”:“ Machine”,“ catDealerCode”:null},{“ id”:“ cbb”, “ regionName”:“韩国”,“ companyName”:“ KR”,“ catDealerCode”:null},{“ id”:“ b6125b0e-5ec9”,“ regionName”:“中国”,“ companyName”:“ CHN” ,“ catDealerCode”:null}],“代码”:0,“ message”:null}
我有类似您上面看到的数据。我根据companyName提取数据。但有些国家没有数据。我想在其中创建一个if情况,但是无论我说element == null还是不接受,我做什么。有人知道我在哪里做错了吗?我应该如何为空数据创建ifif else?
location ~ \.json$ {
root /data/micro/public/;
try_files $uri @json_gen;
}
location @json_gen {
root /data/micro/public/;
# do not include any try_files here for less surprises :)
fastcgi_split_path_info ^(.+\.PHP)(/.+)$;
fastcgi_pass unix:/run/PHP/PHP7.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root/index.PHP;
include fastcgi_params;
}
这是我的国家列表数据;
{“数据”:[{{“代码”:“ KR”,“名称”:“韩国”,“ isActive”:true,“ id”:“ 71”},{“代码”:“ RU” ,“ name”:“ Rusia”,“ isActive”:true,“ id”:“ 3c”},{“ code”:“ Ch”,“ name”:“ China”,“ isActive”:true,“ id” :“ 86”}],“代码”:0,“消息”:null}
onTap: () async {
List<Country> country =
await fetchList(
snapshot.data.code);
country.forEach((element) {
if(element.companyName == null){
print('element is empty');
}else{
print('Here ${element.companyName}');
}
});
},
} }
解决方法
我将使用可识别null的运算符:
onTap: () async {
List<Country> country =
await fetchList(
snapshot.data.code);
country?.forEach((element) {
if(element?.companyName == null){
print('element is empty');
}else{
print('Here ${element.companyName}');
}
});
},