簡體   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