ios 未行程的例外状况:[类型'_InternalLinkedHashMap〈String,dynamic>'不是类型'String'的子类型]

mwkjh3gx  于 2022-12-01  发布在  iOS
关注(0)|答案(2)|浏览(148)

这是json格式,我需要用它来得到数据'

{
    "count": 2,
    "next": null,
    "previous": null,
    "results": [
        {
            "date": "2022-11-23",
            "breaks_set": [],
            "id": "c82af994-541a-40eb-a154-9cf8b130100c",
            "clock_in_time": "2:30",
            "clock_out_time": "6:30",
            "on_time_clock_in": 553,
            "on_time_clock_out": -313
        },
        {
            "date": "2022-11-28",
            "breaks_set": [
                {
                    "start": "09:36:01",
                    "end": "09:40:12.632703",
                    "break_duration": 4
                },
                {
                    "start": "09:40:13.626539",
                    "end": "09:40:14.282107",
                    "break_duration": 0
                },
                {
                    "start": "09:40:14.764177",
                    "end": "09:40:15.606529",
                    "break_duration": 0
                }
            ],
            "id": "e1c21659-1c2f-4ecd-b56b-a45626bedd7c",
            "clock_in_time": "9:36",
            "clock_out_time": "9:40",
            "on_time_clock_in": 128,
            "on_time_clock_out": -124
        }
    ]
}

`
json的模型类的代码如下所示

class BreaksSet {
  String? start;
  String? end;
  int? breakduration;

  BreaksSet({this.start, this.end, this.breakduration});

  BreaksSet.fromJson(Map<String, dynamic> json) {
    start = json['start'];
    end = json['end'];
    breakduration = json['break_duration'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = Map<String, dynamic>();
    data['start'] = start;
    data['end'] = end;
    data['break_duration'] = breakduration;
    return data;
  }
}

class Result {
  String? date;
  List<BreaksSet?>? breaksset;
  String? id;
  String? clockintime;
  String? clockouttime;
  int? ontimeclockin;
  int? ontimeclockout;

  Result(
      {this.date,
      this.breaksset,
      this.id,
      this.clockintime,
      this.clockouttime,
      this.ontimeclockin,
      this.ontimeclockout});

  Result.fromJson(Map<String, dynamic> json) {
    date = json['date'];
    if (json['breaks_set'] != null) {
      breaksset = <BreaksSet>[];
      json['breaks_set'].forEach((v) {
        breaksset!.add(BreaksSet.fromJson(v));
      });
    }
    id = json['id'];
    clockintime = json['clock_in_time'];
    clockouttime = json['clock_out_time'];
    ontimeclockin = json['on_time_clock_in'];
    ontimeclockout = json['on_time_clock_out'];
  }
}

class Attendance {
  int? count;
  String? next;
  String? previous;
  List<Result?>? results;

  Attendance({this.count, this.next, this.previous, this.results});

  Attendance.fromJson(Map<String, dynamic> json) {
    count = json['count'];
    next = json['next'];
    previous = json['previous'];
    if (json['results'] != null) {
      results = <Result>[];
      json['results'].forEach((v) {
        results!.add(Result.fromJson(v));
      });
    }
  }
}

我使用了DIO,方法是,这里我创建了一个连接类,它包含所有类型api调用的dio代码'

Future<List<Attendance>> getUserAttendanceData() async {
    final response = await _connection.getDataWithToken(
      "${KApiUrls.baseUrl}/attendance-list/",
      token,
    );
    if (response != null) {
      if (response.statusCode == 200
      ) {
        var data = jsonDecode(response.data).cast<List<Map<String, dynamic>>>();
        return List.from(
            data.map((attendance) => Attendance.fromJson(attendance)));

       
      } else {
        throw Exception();
      }
    } else {
      throw Error();
    }
  }

`
我得到这个错误,我必须想法如何解决这个问题,但我尝试了几个解决方案

ahy6op9u

ahy6op9u1#

一旦你调用jsonDecode,Map就会被返回,其中可能包含嵌套Map。你可以使用插件在你的模型类中生成toMap(Map<String, dynamic>)方法并使用它。

zy1mlcev

zy1mlcev2#

Future<Attendance> getUserAttendanceData() async {
    final response = await _connection.getDataWithToken(
      "${KApiUrls.baseUrl}/attendance-list/",
      token,
    );
    if (response != null) {
      if (response.statusCode == 200) {
//modified and solved code
       return Attendance.fromJson(response.data);
      } else {
        throw Exception();
      }
    } else {
      throw Error();
    }
  }

我需要对象列表,但得到的对象包含对象列表

相关问题