I have error when convert json to dart object type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String'

Viewed 140

I am trying to get the JSON response from the server and output it to the Dart object.

In this class, I get data from the server:

class GetAllServicePostMethod extends ChangeNotifier {
List<AllServices> _getService;
List<AllServices> get allData => _getService;
set allData(List<AllServices> val) {
_getService = val;
notifyListeners();

}

  Future<List<AllServices>> getInitService() async {
try {
  var url = "url";
  Map<String, String> headers = {
    "auth-token": await SaveData.getDataAuthorization('Success')
  };
  var response = await http.get(url, headers: headers);
  Map<String, dynamic> map = jsonDecode(response.body);
  List<dynamic> res = map["data"];
  List<AllServices> data = [];
  for (var i = 1; i == 1 ; i++) {
    print(allServicesFromJson(res[i]).runtimeType);
    var initData = allServicesFromJson(res[i]);
    data.add(initData);
  }
  allData = data;
  return allData;
} catch (e) {
  print(e);
}

}

and when i want to convert this jeson to the dart objec i have error : type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String'

my postman output from server is like

  Future<List<AllServices>> getInitService() async {
try {
  var url = "http://185.73.112.57/users/services";
  Map<String, String> headers = {
    "auth-token": await SaveData.getDataAuthorization('Success')
  };
  var response = await http.get(url, headers: headers);
  Map<String, dynamic> map = jsonDecode(response.body);
  List<dynamic> res = map["data"];
  List<AllServices> data = [];
  for (var i = 1; i == 1 ; i++) {
    print(allServicesFromJson(res[i]).runtimeType);
    var initData = allServicesFromJson(res[i]);
    data.add(initData);
  }
  allData = data;
  return allData;
} catch (e) {
  print(e);
}

} }

and my response is a json object like :

 "data": [
    {
        "_id": "5fb8afeb10638586f271ee19",
        "serviceName": "a",
        "ambulanceInfo": {
            "_id": "5fb8afeb10638586f271ee1a",
            "services": [
                {
                    "_id": "5fb8afeb10638586f271ee1b",
                    "serviceName": "code",
                    "status": true
                },
                {
                    "_id": "5fb8afeb10638586f271ee1c",
                    "serviceName": "withoutcode",
                    "status": true
                }
            ],
            "percent": 30
        },
        "sendSMS": true,
        "sendEmail": true,
        "status": true,
        "__v": 0
    },
    {
        "_id": "5fb8b03210638586f271ee24",
        "serviceName": "b",
        "laboratoryInfo": {
            "_id": "5fb8b03210638586f271ee25",
            "services": [
                {
                    "_id": "5fb8b03210638586f271ee26",
                    "serviceName": " test",
                    "status": true
                }
            ],
            "percent": 30
        },
        "sendSMS": true,
        "sendEmail": true,
        "status": true,
        "__v": 0
    },

can any one help me to convert this?

1 Answers

Your response model should look like this:

import 'dart:convert';

ResponseModel responseModelFromJson(String str) => ResponseModel.fromJson(json.decode(str));

String responseModelToJson(ResponseModel data) => json.encode(data.toJson());

class ResponseModel {
    ResponseModel({
        this.data,
    });

    List<Datum> data;

    factory ResponseModel.fromJson(Map<String, dynamic> json) => ResponseModel(
        data: json["data"] == null ? null : List<Datum>.from(json["data"].map((x) => Datum.fromJson(x))),
    );

    Map<String, dynamic> toJson() => {
        "data": data == null ? null : List<dynamic>.from(data.map((x) => x.toJson())),
    };
}

class Datum {
    Datum({
        this.id,
        this.serviceName,
        this.ambulanceInfo,
        this.sendSms,
        this.sendEmail,
        this.status,
        this.v,
        this.laboratoryInfo,
    });

    String id;
    String serviceName;
    Info ambulanceInfo;
    bool sendSms;
    bool sendEmail;
    bool status;
    int v;
    Info laboratoryInfo;

    factory Datum.fromJson(Map<String, dynamic> json) => Datum(
        id: json["_id"] == null ? null : json["_id"],
        serviceName: json["serviceName"] == null ? null : json["serviceName"],
        ambulanceInfo: json["ambulanceInfo"] == null ? null : Info.fromJson(json["ambulanceInfo"]),
        sendSms: json["sendSMS"] == null ? null : json["sendSMS"],
        sendEmail: json["sendEmail"] == null ? null : json["sendEmail"],
        status: json["status"] == null ? null : json["status"],
        v: json["__v"] == null ? null : json["__v"],
        laboratoryInfo: json["laboratoryInfo"] == null ? null : Info.fromJson(json["laboratoryInfo"]),
    );

    Map<String, dynamic> toJson() => {
        "_id": id == null ? null : id,
        "serviceName": serviceName == null ? null : serviceName,
        "ambulanceInfo": ambulanceInfo == null ? null : ambulanceInfo.toJson(),
        "sendSMS": sendSms == null ? null : sendSms,
        "sendEmail": sendEmail == null ? null : sendEmail,
        "status": status == null ? null : status,
        "__v": v == null ? null : v,
        "laboratoryInfo": laboratoryInfo == null ? null : laboratoryInfo.toJson(),
    };
}

class Info {
    Info({
        this.id,
        this.services,
        this.percent,
    });

    String id;
    List<Service> services;
    int percent;

    factory Info.fromJson(Map<String, dynamic> json) => Info(
        id: json["_id"] == null ? null : json["_id"],
        services: json["services"] == null ? null : List<Service>.from(json["services"].map((x) => Service.fromJson(x))),
        percent: json["percent"] == null ? null : json["percent"],
    );

    Map<String, dynamic> toJson() => {
        "_id": id == null ? null : id,
        "services": services == null ? null : List<dynamic>.from(services.map((x) => x.toJson())),
        "percent": percent == null ? null : percent,
    };
}

class Service {
    Service({
        this.id,
        this.serviceName,
        this.status,
    });

    String id;
    String serviceName;
    bool status;

    factory Service.fromJson(Map<String, dynamic> json) => Service(
        id: json["_id"] == null ? null : json["_id"],
        serviceName: json["serviceName"] == null ? null : json["serviceName"],
        status: json["status"] == null ? null : json["status"],
    );

    Map<String, dynamic> toJson() => {
        "_id": id == null ? null : id,
        "serviceName": serviceName == null ? null : serviceName,
        "status": status == null ? null : status,
    };
}

You can use this website to convert your JSON to a Dart model.

Related