【问题标题】:I have error when convert json to dart object type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String'将 json 转换为 dart 对象类型“_InternalLinkedHashMap<String, dynamic>”时出现错误不是“String”类型的子类型
【发布时间】:2021-03-09 11:45:12
【问题描述】:

我正在尝试从服务器获取 JSON 响应并将其输出到 Dart 对象。

在这个类中,我从服务器获取数据:

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

}

当我想将此 jeson 转换为 dart 对象时出现错误:type '_InternalLinkedHashMap' is not a subtype of type 'String'

我的邮递员从服务器输出就像

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

} }

我的响应是一个 json 对象,例如:

 "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
    },

谁能帮我转换一下?

【问题讨论】:

    标签: android flutter jwt


    【解决方案1】:

    您的响应模型应如下所示:

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

    您可以使用此website 将您的 JSON 转换为 Dart 模型。

    【讨论】:

      猜你喜欢
      • 2021-03-04
      • 2021-09-10
      • 2021-02-01
      • 2021-02-17
      • 2019-09-19
      • 2021-01-14
      • 2019-08-28
      • 2019-03-23
      • 2021-04-07
      相关资源
      最近更新 更多