简体   繁体   English

未处理的异常:[type '_InternalLinkedHashMap<string, dynamic> ' 不是 'String' 类型的子类型]</string,>

[英]Unhandled exception: [type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String']

this is the json formate by which I need to get the data `这是我需要获取数据的 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
        }
    ]
}

` `

The model class of the json is coded like this json的model class这样编码

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));
      });
    }
  }
}

the api calling I used DIO and the method is, here I made a connection class that contains the dio codes of all type api calling ` api 调用我使用了 DIO,方法是,这里我建立了一个连接 class,其中包含所有类型 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();
    }
  }

` `

I am getting this error, I have to idea how to solve this, but I tried several solution for this我收到这个错误,我必须想办法解决这个问题,但我为此尝试了几种解决方案

Once you call jsonDecode Map is retuned which could contain nested Map. You can use plugin to generate toMap(Map<String, dynamic>) method in your model class and use it.一旦你调用jsonDecode Map 就会被重新调整,它可能包含嵌套的 Map。你可以使用插件在你的 model class 中生成toMap(Map<String, dynamic>)方法并使用它。

    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();
    }
  }

I am expecting of list of objects but I got a object that contains list of objects我期待对象列表,但我得到一个包含对象列表的 object

暂无
暂无

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

相关问题 未处理的异常:类型 '_InternalLinkedHashMap<string, dynamic> ' 不是“ReferralPolicyResponse”类型的子类型? Flutter</string,> - Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'ReferralPolicyResponse?' Flutter 未处理的异常:类型 '_InternalLinkedHashMap<dynamic, dynamic> ' 不是类型 'Map 的子类型<string, dynamic> '?</string,></dynamic,> - Unhandled Exception: type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, dynamic>'? 未处理的异常:类型 '_InternalLinkedHashMap<string, dynamic> ' 不是类型转换 int flutter 中类型 'OriginDestination' 的子类型</string,> - Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'OriginDestination' in type cast int flutter 未处理的异常:类型 '_InternalLinkedHashMap<string, dynamic> ' 不是类型 'Iterable 的子类型<dynamic> '为什么会出现这个错误</dynamic></string,> - Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Iterable<dynamic>' why this error coming 输入'_InternalLinkedHashMap<string, dynamic> ' 不是类型转换中类型 'Session' 的子类型</string,> - type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Session' in type cast _InternalLinkedHashMap <String, dynamic> &#39;不是&#39;List类型的子类型 <Map<dynamic, dynamic> &gt;” - _InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<Map<dynamic, dynamic>>' 未处理的异常:键入“列表”<dynamic> ' 不是类型 'Map 的子类型<string, dynamic> ' 在飞镖/ flutter</string,></dynamic> - Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>' in dart/ flutter 在列表中<user> users = [] ------ 未处理的异常:类型 '(dynamic) =&gt; Null' 不是类型 '(String, dynamic) =&gt; void' of 'f' 的子类型</user> - in List<User> users = [] ------ Unhandled Exception: type '(dynamic) => Null' is not a subtype of type '(String, dynamic) => void' of 'f' 将 json 转换为 dart object 类型 '_InternalLinkedHashMap 时出现错误<string, dynamic> ' 不是类型 'String' 的子类型</string,> - I have error when convert json to dart object type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String' Flutter 上的 json 解析错误。 输入&#39;_InternalLinkedHashMap<String, dynamic> &#39; 不是类型转换中类型 &#39;ResultData&#39; 的子类型 - Error in json parsing on flutter. type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'ResultData' in type cast
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM