从 api 检索某些东西时,我应该为每个空字段设置一个值吗?或者我应该保持它们为空并在显示它们时检查它们是否不为空?
前任:
factory Geo.fromJson(Map<String, dynamic> json) => Geo(
type: json["type"] == null ? 'Point' : json["type"],
coordinates: json["coordinates"] == null ? [] : List<double>.from(json["coordinates"].map((x) => double.tryParse(x))),
);
或者
factory Geo.fromJson(Map<String, dynamic> json) => Geo(
type: json["type"] == null ? null : json["type"],
coordinates: json["coordinates"] == null ? null : List<double>.from(json["coordinates"].map((x) => double.tryParse(x))),
);
编辑->或者我应该以其他方式这样做吗?
factory Location.fromJson(Map<String, dynamic> json) {
return Location(
geo: json["geo"] = Geo.fromJson(json["geo"]),
city: json["city"] = json["city"],
country: json["country"] = json["country"],
area: json["area"] = json["area"],
);
}