在flutter中,无法将参数类型'Object''分配给参数类型'Map <String,dynamic>'

时间:2020-03-30 23:33:17

标签: json flutter dart

我有一个问题。是初学者。我试图用颤动来解析一些json。这是我的模型课。

JsonData

{
    "status": "success",
    "data": [
        {
            "id": "02ab1ef0-6fa9-11ea-ba0f-b1d475e9cef4",
            "policy_name": "Third Party Insurance",
            "description": "This is Third Party Insurance",
            "status": 1,
            "deleted_at": null,
            "created_at": "2020-03-26 21:30:27",
            "updated_at": "2020-03-26 21:30:27",
            "insurance_policy_type": [
                {
                    "id": "3c674550-6faa-11ea-b97c-9d7c2598dab0",
                    "insurance_policy_id": "02ab1ef0-6fa9-11ea-ba0f-b1d475e9cef4",
                    "insurance_company_id": "07a53f10-6fa8-11ea-bb6d-89fb742a644e",
                    "policy_type_name": "Third Party - Grade A",
                    "description": "Third Party - Grade A. Third Party - Grade A. Third Party - Grade A",
                    "benefit": "Benefit of Third Party - Grade A. This is going to be a long text",
                    "amount": 5000,
                    "commission": 300,
                    "status": 1,
                    "deleted_at": null,
                    "created_at": "2020-03-26 21:39:13",
                    "updated_at": "2020-03-26 21:39:13"
                },
                {
                    "id": "4ec7a4a0-6faa-11ea-9739-77f0ed3fc12c",
                    "insurance_policy_id": "02ab1ef0-6fa9-11ea-ba0f-b1d475e9cef4",
                    "insurance_company_id": "07a53f10-6fa8-11ea-bb6d-89fb742a644e",
                    "policy_type_name": "Third Party - Grade B",
                    "description": "Third Party - Grade B. Third Party - Grade B. Third Party - Grade B",
                    "benefit": "Benefit of Third Party - Grade B. This is going to be a long text",
                    "amount": 8000,
                    "commission": 500,
                    "status": 1,
                    "deleted_at": null,
                    "created_at": "2020-03-26 21:39:44",
                    "updated_at": "2020-03-26 21:39:44"
                }
            ]
        },
        {
            "id": "10e31df0-6fa9-11ea-962f-e5df8d00bea3",
            "policy_name": "Personal Accident Insurance",
            "description": "This is Personal Accident Insurance",
            "status": 1,
            "deleted_at": null,
            "created_at": "2020-03-26 21:30:51",
            "updated_at": "2020-03-26 21:30:51",
            "insurance_policy_type": [
                {
                    "id": "aaf3ef10-6fb2-11ea-8c17-8de2cd67bf64",
                    "insurance_policy_id": "10e31df0-6fa9-11ea-962f-e5df8d00bea3",
                    "insurance_company_id": "07a53f10-6fa8-11ea-bb6d-89fb742a644e",
                    "policy_type_name": "Personal Accident - Grade A",
                    "description": "Personal Accident - Grade A. Personal Accident - Grade A. Personal Accident - Grade A",
                    "benefit": "Benefit of Personal Accident - Grade A. This is going to be a long text",
                    "amount": 4000,
                    "commission": 500,
                    "status": 1,
                    "deleted_at": null,
                    "created_at": "2020-03-26 22:39:35",
                    "updated_at": "2020-03-26 22:39:35"
                },
                {
                    "id": "b4835d00-6fb2-11ea-a48f-c3481acd8970",
                    "insurance_policy_id": "10e31df0-6fa9-11ea-962f-e5df8d00bea3",
                    "insurance_company_id": "07a53f10-6fa8-11ea-bb6d-89fb742a644e",
                    "policy_type_name": "Personal Accident - Grade B",
                    "description": "Personal Accident - Grade B. Personal Accident - Grade B. Personal Accident - Grade B",
                    "benefit": "Benefit of Personal Accident - Grade B. This is going to be a long text",
                    "amount": 4000,
                    "commission": 500,
                    "status": 1,
                    "deleted_at": null,
                    "created_at": "2020-03-26 22:39:51",
                    "updated_at": "2020-03-26 22:39:51"
                },
                {
                    "id": "c6d84e60-6fb2-11ea-a773-497e66c9ab29",
                    "insurance_policy_id": "10e31df0-6fa9-11ea-962f-e5df8d00bea3",
                    "insurance_company_id": "07a53f10-6fa8-11ea-bb6d-89fb742a644e",
                    "policy_type_name": "Personal Accident - Grade C",
                    "description": "Personal Accident - Grade C. Personal Accident - Grade C. Personal Accident - Grade C",
                    "benefit": "Benefit of Personal Accident - Grade C. This is going to be a long text",
                    "amount": 10000,
                    "commission": 1000,
                    "status": 1,
                    "deleted_at": null,
                    "created_at": "2020-03-26 22:40:21",
                    "updated_at": "2020-03-26 22:40:21"
                }
            ]
        },
        {
            "id": "1a727fc0-6fa9-11ea-a43f-b52dc75efa00",
            "policy_name": "Household Insurance",
            "description": "This is Household Insurance",
            "status": 1,
            "deleted_at": null,
            "created_at": "2020-03-26 21:31:07",
            "updated_at": "2020-03-26 21:34:17",
            "insurance_policy_type": [
                {
                    "id": "016fa680-6fb3-11ea-93b7-3fdae3d5875a",
                    "insurance_policy_id": "1a727fc0-6fa9-11ea-a43f-b52dc75efa00",
                    "insurance_company_id": "07a53f10-6fa8-11ea-bb6d-89fb742a644e",
                    "policy_type_name": "Household Insurance - Grade D",
                    "description": "Household Insurance - Grade D. Household Insurance - Grade D. Household Insurance - Grade D",
                    "benefit": "Benefit of Household Insurance - Grade D. This is going to be a long text",
                    "amount": 9000,
                    "commission": 1000,
                    "status": 1,
                    "deleted_at": null,
                    "created_at": "2020-03-26 22:42:00",
                    "updated_at": "2020-03-26 22:42:00"
                },
                {
                    "id": "13a62720-6fb3-11ea-8842-ed9732bb7c86",
                    "insurance_policy_id": "1a727fc0-6fa9-11ea-a43f-b52dc75efa00",
                    "insurance_company_id": "07a53f10-6fa8-11ea-bb6d-89fb742a644e",
                    "policy_type_name": "Household Insurance - Grade K",
                    "description": "Household Insurance - Grade K. Household Insurance - Grade K. Household Insurance - Grade K",
                    "benefit": "Benefit of Household Insurance - Grade K. This is going to be a long text",
                    "amount": 7500,
                    "commission": 600,
                    "status": 1,
                    "deleted_at": null,
                    "created_at": "2020-03-26 22:42:30",
                    "updated_at": "2020-03-26 22:42:30"
                },
                {
                    "id": "f4ceb490-6fb2-11ea-9b04-a1229a2e2128",
                    "insurance_policy_id": "1a727fc0-6fa9-11ea-a43f-b52dc75efa00",
                    "insurance_company_id": "07a53f10-6fa8-11ea-bb6d-89fb742a644e",
                    "policy_type_name": "Household Insurance - Grade C",
                    "description": "Household Insurance - Grade C. Household Insurance - Grade C. Household Insurance - Grade C",
                    "benefit": "Benefit of Household Insurance - Grade C. This is going to be a long text",
                    "amount": 9000,
                    "commission": 1000,
                    "status": 1,
                    "deleted_at": null,
                    "created_at": "2020-03-26 22:41:39",
                    "updated_at": "2020-03-26 22:41:39"
                }
            ]
        }
    ]
}

模型

class Insurance {
  String status;
  List<Data> data;

  Insurance({this.status, this.data});

  Insurance.fromJson(Map<String, dynamic> json) {
    status = json['status'];
    if (json['data'] != null) {
      data = new List<Data>();
      json['data'].forEach((v) {
        data.add(new Data.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['status'] = this.status;
    if (this.data != null) {
      data['data'] = this.data.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class Data {
  String id;
  String policyName;
  String description;
  int status;
  var deletedAt;
  String createdAt;
  String updatedAt;
  List<InsurancePolicyType> insurancePolicyType;

  Data(
      {this.id,
        this.policyName,
        this.description,
        this.status,
        this.deletedAt,
        this.createdAt,
        this.updatedAt,
        this.insurancePolicyType});

  Data.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    policyName = json['policy_name'];
    description = json['description'];
    status = json['status'];
    deletedAt = json['deleted_at'];
    createdAt = json['created_at'];
    updatedAt = json['updated_at'];
    if (json['insurance_policy_type'] != null) {
      insurancePolicyType = new List<InsurancePolicyType>();
      json['insurance_policy_type'].forEach((v) {
        insurancePolicyType.add(new InsurancePolicyType.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['policy_name'] = this.policyName;
    data['description'] = this.description;
    data['status'] = this.status;
    data['deleted_at'] = this.deletedAt;
    data['created_at'] = this.createdAt;
    data['updated_at'] = this.updatedAt;
    if (this.insurancePolicyType != null) {
      data['insurance_policy_type'] =
          this.insurancePolicyType.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class InsurancePolicyType {
  String id;
  String insurancePolicyId;
  String insuranceCompanyId;
  String policyTypeName;
  String description;
  String benefit;
  int amount;
  int commission;
  int status;
  Null deletedAt;
  String createdAt;
  String updatedAt;

  InsurancePolicyType(
      {this.id,
        this.insurancePolicyId,
        this.insuranceCompanyId,
        this.policyTypeName,
        this.description,
        this.benefit,
        this.amount,
        this.commission,
        this.status,
        this.deletedAt,
        this.createdAt,
        this.updatedAt});

  InsurancePolicyType.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    insurancePolicyId = json['insurance_policy_id'];
    insuranceCompanyId = json['insurance_company_id'];
    policyTypeName = json['policy_type_name'];
    description = json['description'];
    benefit = json['benefit'];
    amount = json['amount'];
    commission = json['commission'];
    status = json['status'];
    deletedAt = json['deleted_at'];
    createdAt = json['created_at'];
    updatedAt = json['updated_at'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['insurance_policy_id'] = this.insurancePolicyId;
    data['insurance_company_id'] = this.insuranceCompanyId;
    data['policy_type_name'] = this.policyTypeName;
    data['description'] = this.description;
    data['benefit'] = this.benefit;
    data['amount'] = this.amount;
    data['commission'] = this.commission;
    data['status'] = this.status;
    data['deleted_at'] = this.deletedAt;
    data['created_at'] = this.createdAt;
    data['updated_at'] = this.updatedAt;
    return data;
  }
}

现在,我要保存insurancePolicyType类型的List<InsurancePolicyType>,但不能保存。

main.dart

  List<InsurancePolicyType> insurancePolicyType;
    List<Data> insuranceDataList = [];
    ....


     fetchInsuranceList() async{
          final responseJson = json.decode(response.body);
          setState(() {
            insuranceDataList = List<Data>.from(
                responseJson["data"].map((i) => Data.fromJson(i)));
    //so this is where I get the error.
            insurancePolicyType = insuranceDataList.map((i) => InsurancePolicyType.fromJson(i)).toList();

      }

那么,如何从insurancePolicyType中获取insuranceDataList的列表?

0 个答案:

没有答案