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

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

Problem Description:

this is the json formate by which I need to get the data `

{
    "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

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
`

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

Solution – 1

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.

Solution – 2

    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

Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Reject