I am trying to convert JSON response to dart but over two error
1) A value of type 'dynamic' can't be assigned to a variable of type 'List?
2) The argument type 'dynamic' can't be assigned to the parameter type 'Map<String, dynamic>
Thanks in advance
I have posted the code for your reference.
class GetImProfileDetails {
List<Params>? params;
String? status;
String? statusDesc;
GetImProfileDetails({this.params, this.status, this.statusDesc});
GetImProfileDetails.fromJson(Map<String, dynamic> json) {
if (json['params'] != null) {
params = <Params>[];
json['params'].forEach((v) {
params!.add(new Params.fromJson(v)); // Second Error on V
});
}
status = json['status'] as String;
statusDesc = json['statusDesc'] as String;
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.params != null) {
data['params'] = this.params!.map((v) => v.toJson()).toList();
}
data['status'] = this.status;
data['statusDesc'] = this.statusDesc;
return data;
}
}
class Params {
String? head;
String? headCode;
List<String>? options;
Params({this.head, this.headCode, this.options});
Params.fromJson(Map<String, dynamic> json) {
head = json['head'] as String;
headCode = json['headCode'] as String;
options = json['options'].cast<String>(); // First Error
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['head'] = this.head;
data['headCode'] = this.headCode;
data['options'] = this.options;
return data;
}
}
My JSON response
{
"params": [
{
"head": "Age",
"headCode": "AG",
"options": [
"AG3~Others~1",
"AG2~45-55~0",
"AG1~30-45~0",
"999999~Select~0"
]
},
{
"head": "Annual Income",
"headCode": "AI",
"options": [
"AI4~>12lacs~1",
"AI3~<4lacs~0",
"AI2~8-12lacs~0",
"AI1~4-8lacs~0",
"999999~Select~0"
]
},
{
"head": "Education",
"headCode": "EDU",
"options": [
"EDU4~Post Graduate~1",
"EDU3~Graduate~0",
"EDU2~Class XII~0",
"EDU1~Class X~0",
"999999~Select~0"
]
},
{
"head": "Present Occupation ",
"headCode": "PO",
"options": [
"PO9~Financial Advisor~0",
"PO8~Employee/owner of corporate client~0",
"PO7~DoctorA ~0",
"PO6~DST~0",
"PO5~DSA~0",
"PO4~Competition Agent~0",
"PO3~Career Agent~0",
"PO27~Used Car DealerA ~0",
"PO26~Unemployed~0",
"PO25~Transporter~0",
"PO24~Student~0",
"PO23~Stock Broker ~0",
"PO22~Service~0",
"PO21~Self-Employed~1",
"PO20~Salaried~0",
"PO2~Business~0",
"PO19~RTO agent~0",
"PO18~Project ConsultantA ~0",
"PO17~Part Time~0",
"PO16~Other financial services~0",
"PO15~New Car DealerA ~0",
"PO14~LI Agent~0",
"PO13~Housewife~0",
"PO12~Garage~0",
"PO11~GI-PVT~0",
"PO10~GI-PSU~0",
"PO1~Aggregator~0",
"999999~Select~0"
]
},
{
"head": "Health PSU Experience",
"headCode": "HPSUE",
"options": [
"HPSUE5~> 10 Years~1",
"HPSUE4~5-10years~0",
"HPSUE3~2-5Years~0",
"HPSUE2~1-2 Years~0",
"HPSUE1~0-1year~0",
"999999~Select~0"
]
},
{
"head": "Health Pvt Experience",
"headCode": "HPVTE",
"options": [
"HPSUE9~5-10years~1",
"HPSUE8~2-5Years~0",
"HPSUE7~1-2 Years~0",
"HPSUE6~0-1year~0",
"HPSUE10~> 10 Years~0",
"999999~Select~0"
]
},
{
"head": "LI PSU Experience",
"headCode": "LPSUE",
"options": [
"LPSUE5~> 10 Years~1",
"LPSUE4~5-10years~0",
"LPSUE3~2-5Years~0",
"LPSUE2~1-2 Years~0",
"LPSUE1~0-1year~0",
"999999~Select~0"
]
},
{
"head": "LI Pvt Experience",
"headCode": "LPVTE",
"options": [
"LPSUE9~5-10years~1",
"LPSUE8~2-5Years~0",
"LPSUE7~1-2 Years~0",
"LPSUE6~0-1year~0",
"LPSUE10~> 10 Years~0",
"999999~Select~0"
]
},
{
"head": "No. of years in the city",
"headCode": "NYIC",
"options": [
"NYIC3~> 7 Years~1",
"NYIC2~<3 Years~0",
"NYIC1~3-7 Years~0",
"999999~Select~0"
]
},
{
"head": "GI Annual Potential",
"headCode": "GANP",
"options": [
"GANP7~>100 lakhs~1",
"GANP6~<3 lakhs~0",
"GANP5~50-100 lakhs~0",
"GANP4~5-10 lakhs~0",
"GANP3~3-5 lakhs~0",
"GANP2~25-50 lakhs~0",
"GANP1~10-25 lakhs~0",
"999999~Select~0"
]
},
{
"head": "Health Annual Potential",
"headCode": "HANP",
"options": [
"HANP7~>100 lakhs~1",
"HANP6~<3 lakhs~0",
"HANP5~50-100 lakhs~0",
"HANP4~5-10 lakhs~0",
"HANP3~3-5 lakhs~0",
"HANP2~25-50 lakhs~0",
"HANP1~10-25 lakhs~0",
"999999~Select~0"
]
},
{
"head": "Motor Annual Potential",
"headCode": "MAP",
"options": [
"MAP7~>100 lakhs~1",
"MAP6~<3 lakhs~0",
"MAP5~50-100 lakhs~0",
"MAP4~5-10 lakhs~0",
"MAP3~3-5 lakhs~0",
"MAP2~25-50 lakhs~0",
"MAP1~10-25 lakhs~0",
"999999~Select~0"
]
},
{
"head": "Potential Club",
"headCode": "PC",
"options": [
"PC5~Silver~0",
"PC4~Platinum~0",
"PC3~Partner~0",
"PC2~Gold~1",
"PC1~CEO~0",
"999999~Select~0"
]
},
{
"head": "SME Annual Potential",
"headCode": "SNP",
"options": [
"SNP7~>100 lakhs~1",
"SNP6~<3 lakhs~0",
"SNP5~50-100 lakhs~0",
"SNP4~5-10 lakhs~0",
"SNP3~3-5 lakhs~0",
"SNP2~25-50 lakhs~0",
"SNP1~10-25 lakhs~0",
"999999~Select~0"
]
}
],
"status": "Success",
"statusDesc": "1"
}
CodePudding user response:
To solve the 2nd error if think you have to do:
json['params'].forEach((Map<String, dynamic> v) { //rest of code
CodePudding user response:
For the first error, you are initializing the string list in a wrong way, change your code from
Params.fromJson(Map<String, dynamic> json) {
head = json['head'] as String;
headCode = json['headCode'] as String;
options = json['options'].cast<String>(); // First Error
}
//to:
Params.fromJson(Map<String, dynamic> json) {
head = json['head'] as String;
headCode = json['headCode'] as String;
options = List<String>.from(json['options']);
}
For the second error:
if (json['params'] != null) {
params = <Params>[];
json['params'].forEach((v) {
params!.add(new Params.fromJson(v)); // Second Error on V
});
}
//Change it to:
if (json['params'] != null) {
params = <Params>[];
for (final param in json['params'] ?? [])
params!.add(new Params.fromJson(param));
}