Flutter if-else statement



{quot;数据":[{quotquot;id":"32f","regionName":"Korea","companyName":"Machine","catDealerCode":null},{"companyName":"KR"、"catDealerCode":null},{"id":"b6125b0e-5ec9",,"regionName":"China","companyName":"CHN","catDealerCode":null}],";代码":0;消息":null}

我有你上面看到的数据。我根据companyName提取数据。但有些国家没有数据。我想在其中创建一个if-else案例。但无论我做什么,当我说element==null时,它都不接受。有人知道我哪里做错了吗?我应该如何为空数据创建if-else?

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}');
}
});
},

这是我的国家名单数据;

{数据:[{代码:KR,名称:韩国,isActive:真,id:71":"3c"},{"代码":"Ch","名称":"中国","isActive":true,"id":"86"}],"代码":0;消息":null}

class Country {
String id;
String companyCode;
String countryCode;
String countryId;
String regionName;
String companyName;
Null catDealerCode;
Country(
{this.id,
this.companyCode,
this.countryCode,
this.countryId,
this.regionName,
this.companyName,
this.catDealerCode});
Couuntry.fromJson(Map<String, dynamic> json) {
id = json['id'];
companyCode = json['companyCode'];
countryCode = json['countryCode'];
countryId = json['countryId'];
regionName = json['regionName'];
companyName = json['companyName'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['companyCode'] = this.companyCode;
data['countryCode'] = this.countryCode;
data['countryId'] = this.countryId;
data['regionName'] = this.regionName;
data['companyName'] = this.companyName;
return data;

}}

我会使用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}');
}
});
},

最新更新