尝试创建一个if语句以检查API是否返回包含数据的“items”
这是API网址,您可以看到该特定数据查询的items为空https://data.food.gov.uk/food-alerts/id?since=2021-01-04T00:00:00Z
下面是我的代码,一直尝试检查'items'是否为null,还要检查其长度并查看是否等于0,但迄今为止都没有成功
class AllergyAlert {
final String title;
AllergyAlert({this.title});
factory AllergyAlert.fromJson(Map<String, dynamic> json) {
if (json['items'] == null) {
return AllergyAlert(
title: 'No new allergy alerts',
);
} else {
return AllergyAlert(
title: json['items'][0]['title'],
);
}
}
}