Flutter Dart:如果JSON不';不符合对象定义
假设我有一个定义如下的数据模型:Flutter Dart:如果JSON不';不符合对象定义,flutter,dart,Flutter,Dart,假设我有一个定义如下的数据模型: class GymInfo { final String openDate; final String phoneNumber; final String state; final String clubRegion; final String email; final int hasKeypad; final int isPlatinum; final String linkClub; final int id; fin
class GymInfo {
final String openDate;
final String phoneNumber;
final String state;
final String clubRegion;
final String email;
final int hasKeypad;
final int isPlatinum;
final String linkClub;
final int id;
final int is24Hour;
final String fullname;
final String type;
GymInfo({this.openDate, this.phoneNumber, this.state, this.clubRegion, this.email, this.hasKeypad, this.isPlatinum, this.linkClub, @required this.id, this.is24Hour, @required this.fullname, this.type,});
factory GymInfo.fromJson(Map<String, dynamic> json) {
return GymInfo(
openDate: json['openDate'],
phoneNumber: json["PhoneNumber"],
state: json["state"],
clubRegion: json["ClubRegion"],
email: json['Email'],
hasKeypad: json['hasKeypad'],
isPlatinum: json['isPlatinum'],
linkClub: json['link_club'],
id: json['id'],
is24Hour: json['is24Hour'],
fullname: json['fullname'],
type: json['type']);
}
}
类信息{
最终字符串打开日期;
最终字符串电话号码;
最终字符串状态;
最后的弦乐区;
最终字符串电子邮件;
最终的键盘;
终末内腭;
最后的弦乐俱乐部;
最终int id;
最后的时间是24小时;
最终字符串全名;
最终字符串类型;
GymInfo({this.openDate,this.phoneNumber,this.state,this.clubRegion,this.email,this.haskipad,this.isPlatinum,this.linkClub,@required this.id,this.is24小时,@required this.fullname,this.type,});
factory GymInfo.fromJson(映射json){
返回GymInfo(
openDate:json['openDate'],
phoneNumber:json[“phoneNumber”],
状态:json[“状态”],
clubRegion:json[“clubRegion”],
电子邮件:json['email'],
haskipad:json['haskipad'],
isPlatinum:json['isPlatinum'],
linkClub:json['link_club'],
id:json['id'],
is24Hour:json['is24Hour'],
全名:json['fullname'],
类型:json['type']);
}
}
每当我将JSON映射传递给工厂时,如果映射没有所需参数的值,我希望它返回null。此时,它返回一个GymInfo实例,所有参数都设置为null,这给测试带来了问题。如何确保初始化器失败并返回null。只需添加一个简单的验证,以确保所需字段不为null
factory GymInfo.fromJson(Map<String, dynamic> json) {
final fullnameValue = json['fullname'];
final idValue = json['id'];
if (fullNameValue == null || idValue == null){
return null;
}
return GymInfo(
openDate: json['openDate'],
phoneNumber: json["PhoneNumber"],
state: json["state"],
clubRegion: json["ClubRegion"],
email: json['Email'],
hasKeypad: json['hasKeypad'],
isPlatinum: json['isPlatinum'],
linkClub: json['link_club'],
id: idValue,
is24Hour: json['is24Hour'],
fullname: fullnameValue,
type: json['type']);
}
factory GymInfo.fromJson(映射json){
final fullnameValue=json['fullname'];
最终idValue=json['id'];
if(fullNameValue==null | | idValue==null){
返回null;
}
返回GymInfo(
openDate:json['openDate'],
phoneNumber:json[“phoneNumber”],
状态:json[“状态”],
clubRegion:json[“clubRegion”],
电子邮件:json['email'],
haskipad:json['haskipad'],
isPlatinum:json['isPlatinum'],
linkClub:json['link_club'],
id:idValue,
is24Hour:json['is24Hour'],
fullname:fullnameValue,
类型:json['type']);
}
这比你想象的要容易得多。JSON格式是一种没有任何逻辑的格式。这意味着您应该可以随时更改JSON对象的实现而不受影响。
在这种情况下,在您自己的服务对象中实现所需的逻辑更为正确
MyJsonObject getMyObject(对象源){
// ...
var myObject=MyJsonObject.fromJson(json);
if(myObject.someField==null){
返回null;
}
返回myObject;
}```