Flutter 未处理的异常:DioError[DioErrorType.DEFAULT]:将对象转换为可编码对象失败:实例';日期时间';
我试图发送多个对象,我想通过dio发送,当我尝试这个我得到 未处理的异常:DioError[DioErrorType.DEFAULT]:正在转换 对象到可编码对象失败。“DateTime”的实例 我也尝试过将对象转换为Json,但仍然会出现其他错误 我的模型Flutter 未处理的异常:DioError[DioErrorType.DEFAULT]:将对象转换为可编码对象失败:实例';日期时间';,flutter,dart,dio,Flutter,Dart,Dio,我试图发送多个对象,我想通过dio发送,当我尝试这个我得到 未处理的异常:DioError[DioErrorType.DEFAULT]:正在转换 对象到可编码对象失败。“DateTime”的实例 我也尝试过将对象转换为Json,但仍然会出现其他错误 我的模型 BasicInfoModel basicInfoModelFromJson(String str) => BasicInfoModel.fromJson(json.decode(str)); String basicInfoMode
BasicInfoModel basicInfoModelFromJson(String str) => BasicInfoModel.fromJson(json.decode(str));
String basicInfoModelToJson(BasicInfoModel data) => json.encode(data.toJson());
class BasicInfoModel {
BasicInfoModel({
this.id,
this.monitorName,
this.monitorPhoneno,
this.fieldleadEmpno,
this.fieldleadName,
this.fieldleadPhone,
this.fieldteamId,
this.ea,
this.district,
this.region,
this.date,
this.timeStarted,
this.timeEnded,
});
String id;
String monitorName;
String monitorPhoneno;
String fieldleadEmpno;
String fieldleadName;
String fieldleadPhone;
String fieldteamId;
String ea;
String district;
String region;
DateTime date;
String timeStarted;
String timeEnded;
factory BasicInfoModel.fromJson(Map<String, dynamic> json) => BasicInfoModel(
id: json["id"],
monitorName: json["monitorName"],
monitorPhoneno: json["monitorPhoneno"],
fieldleadEmpno: json["fieldleadEmpno"],
fieldleadName: json["fieldleadName"],
fieldleadPhone: json["fieldleadPhone"],
fieldteamId: json["fieldteamId"],
ea: json["ea"],
district: json["district"],
region: json["region"],
date: DateTime.parse(json["date"]),
timeStarted: json["timeStarted"],
timeEnded: json["timeEnded"],
);
Map<String, dynamic> toJson() => {
"id": id,
"monitorName": monitorName,
"monitorPhoneno": monitorPhoneno,
"fieldleadEmpno": fieldleadEmpno,
"fieldleadName": fieldleadName,
"fieldleadPhone": fieldleadPhone,
"fieldteamId": fieldteamId,
"ea": ea,
"district": district,
"region": region,
"date": date.toIso8601String(),
"timeStarted": timeStarted,
"timeEnded": timeEnded,
};
}
我正在提取的json字符串
[{type: Input, label: Monitor Name, required: true, value: re}, {type: Numeric, label: Monitor Phone No(s), required: true, value: 087}, {type: Numeric, label: Field Team ID*, required: true, value: yt}, {type: Input, label: Field Team Lead Name, required: true, value: yh}, {type: Numeric, label: Field Team Phone No, required: true, value: 89}, {type: Numeric, label: Field Team Employee No, required: true, value: 89}, {type: Input, label: EA*, required: true, value: yh}, {type: Input, label: District*, required: true, value: gy}, {type: Input, label: Region, required: true, value: g}, {type: Input, label: Time started, required: true, value: g}, {type: Input, label: Time ended, required: true, value: b}, {type: DatePicker, label: Date, required: true, value: 2020-11-14 18:32:06.176338}]
json[“date”]
看起来怎么样?如果它的null或值无法正确解析,它将抛出FormatException
首先尝试检查空值:
date: json["date"] == null ? null : DateTime.parse(json["date"]),
还要检查json[“date”]的格式,可能是从Epoch开始的毫秒数Hi@EdwynZN这是json它不是empy
{type:DatePicker,label:date,required:true,value:2020-11-13 08:06:18.99}
它有毫秒我不完全理解,它是一个带有一个名为“value”的键的映射,在一个日期字符串中,键“date”在哪里?我已经共享了整个json字符串我没有看到响应、BasicInfo模型和提取值之间的关系,在您询问响应是否为DateTime类型的日期中,返回它的值,否则返回…它的值?但很明显,该值不会被解释为第二个选项的日期时间。如果是字符串,请将其作为final date=responses[0]['fields'][11]['value']运行;并检查其类型timeStarted
和timeEnded
的类型是什么?它们是DateTime
的对象吗?不,它们不是@RaviSinghLodhi字符串basicInfoModelToJson(basicinfomodeldata)=>json.encode(data.toJson())
将此函数转换为块函数:String basicInfoModelToJson(BasicInfoModel数据){String json=data.toJson();print(json);尝试{return json.encode(data.toJson());}catch(e){print(e.toString());}}
也尝试调试以确定是哪个参数导致此问题。Hi RaviSinghLodhi
date: json["date"] == null ? null : DateTime.parse(json["date"]),