Solution 1 :
Try this
var response = jsonDecode(response.body);
List installmentList = List.from(response["installment"]);
setState((){
_list = installmentList.map((f) => PaymentInstallment.fromJson(f)).toList();
});
If it isn’t looping try this
List tempList = [];
installmentList.forEach((f) => tempList.add(f));
setState((){
_list = tempList;
});
Problem :
This is the API response I want to fetch “installments” array values
{
"unit":
{
.....
},
"receipt":
{
.....
},
"instalments": [
{
"payment_type": "5",
"amount": "3",
"bank_name": "ABU DHABI COMMERCIAL BANK",
"paid_date": "2020-08-07 01:11:02",
"status": "0",
}
{
"payment_type": "5",
"amount": "3",
"bank_name": "ABU DHABI COMMERCIAL BANK",
"paid_date": "2020-08-07 01:11:02",
"status": "0",
}
]
}
I use this mode class to get value from API
class PaymentInstallment {
String payment_type;
String payment_amount;
String payment_date;
String status;
PaymentInstallment(
{this.payment_type, this.payment_amount, this.payment_date, this.status});
factory PaymentInstallment.fromjson(Map<String, dynamic> json) {
return PaymentInstallment(
payment_type: json['payment_type'],
payment_amount: json['amount'],
payment_date: json['paid_date'],
status: json['status'],
);
}
}
this is my list and adding the response from the server to it
List<PaymentInstallment> _list = [];
final response = await http.get(
url,
headers: {"Accept": "application/json", "Authorization": token},
);
var response = jsonDecode(response.body);
setState(() {
for (Map temppayment in response) {
_list.add(PaymentInstallment.fromjson(temppayment));
}
});
how can i all array value from “instalments” array and add to the list
Comments
Comment posted by Josteve
The fields from the json array “installment” are cleary different from what you have in your PaymentInstallmet class
Comment posted by Marzook
inside there is the value I posted only a few
Comment posted by Krish Bhanushali
response["installments"]
Comment posted by Marzook
thanks , but it’s showing only the first index value on how to loop it?
Comment posted by Josteve
Sorry @Marzook I don’t get your question
Comment posted by Josteve
@Marzook check the other option. I’ve updated my answer