繁体   English   中英

未处理的异常:DioError [DioErrorType.DEFAULT]:将对象转换为可编码的 object 失败:“DateTime”实例

[英]Unhandled Exception: DioError [DioErrorType.DEFAULT]: Converting objects to an encodable object failed: Instance of 'DateTime'

我正在尝试发送多个我想通过 dio 发送的 object 当我尝试这个时我得到

未处理的异常:DioError [DioErrorType.DEFAULT]:将 object 转换为可编码的 object 失败。“DateTime”实例

我也试过将 object 转换为 Json 但我仍然遇到其他错误

我的 Model

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 项

 final String id = uuid;
    final String monitorName = responses[0]['fields'][0]['value'];
    final String monitorPhoneno = responses[0]['fields'][1]['value'];
    final String fieldleadEmpno = responses[0]['fields'][2]['value'];
    final String fieldleadName = responses[0]['fields'][3]['value'];
    final String fieldleadPhone = responses[0]['fields'][4]['value'];
    final String fieldteamId = responses[0]['fields'][5]['value'];
    final String ea = responses[0]['fields'][6]['value'];
    final String district = responses[0]['fields'][7]['value'];
    final String region = responses[0]['fields'][8]['value'];
    final DateTime date = (responses[0]['fields'][11]['value'] is DateTime)
        ? responses[0]['fields'][11]['value']
        : responses[0]['fields'][11]['value'];
    final String timeStarted = responses[0]['fields'][9]['value'];
    final String timeEnded = responses[0]['fields'][10]['value'];

我正在提取的 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

首先尝试检查 null:

date: json["date"] == null ? null : DateTime.parse(json["date"]),

还要检查 json["date"] 的格式,也许它是自 Epoch 以来的毫秒数

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM