Hello i am trying to save my request into my List but it says that Unhandled Exception: type 'String' is not a subtype of type 'int' of 'index'.
Hello i am trying to save my request into my List but it says that Unhandled Exception: type 'String' is not a subtype of type 'int' of 'index'.
Here is my Class:
class Evsebill {
final String serial;
final double value;
final double vat;
final double total;
final int energy;
const Evsebill({
required this.serial,
required this.value,
required this.vat,
required this.total,
required this.energy
});
factory Evsebill.fromJson(Map<String, dynamic> json) {
return Evsebill(
serial: json['serial'] as String,
value: json['value']as double,
vat: json['vat']as double,
total: json['total']as double,
energy: json['energy']as int,
);
}
}
Here is my request:
List<Evsebill> parseBills(String responseBody) {
final parsed = jsonDecode(responseBody)["results"]["cardBillsTotal"].cast<Map<String, dynamic>>();
return parsed.map<Evsebill>((json) => Evsebill.fromJson(json)).toList();
}
Future<List<Evsebill>> fetch() async {
String? token = await this.storage.read(key: "token");
Map<String, String> headers = {
"Content-Type": "application/json",
"Accept": "application/json",
"Authorization": "Bearer " (token ?? ""),
};
final response = await http.get(Uri.parse(
this.serverIP ':' this.serverPort
'/user/contractedChargeTransactionsList?page=1&limit=10&year=eq:2022'),
headers: headers);
if (response.statusCode == 200) {
setState(() {
print(response.body);
cardBills = jsonDecode(response.body)["results"]["cardBillsTotal"] as List;
var result = cardBills.map((e) => Evsebill.fromJson(e)).toList();
});
return cardBills.map((e) => Evsebill.fromJson(e)).toList();
}
else{
throw Exception('Failed to load Bills');
}
}
here is my print(response.body);
I/flutter (19587): {"results":[{"firstName":"Θωμάς","lastName":"Παπαϊωάννου","userID":238,"month":5,"year":2022,"cardBillsTotal":[{"serial":"884221337251","value":1.0450,"vat":0.2508,"total":1.2958,"energy":0}]},{"firstName":"Θωμάς","lastName":"Παπαϊωάννου","userID":238,"month":6,"year":2022,"cardBillsTotal":[{"serial":"884221337251","value":3.4034,"vat":0.8168,"total":4.2202,"energy":0}]},{"firstName":"Θωμάς","lastName":"Παπαϊωάννου","userID":238,"month":7,"year":2022,"cardBillsTotal":[{"serial":"884221337251","value":2.0900,"vat":0.5016,"total":2.5916,"energy":0}]},{"firstName":"Θωμάς","lastName":"Παπαϊωάννου","userID":238,"month":5,"year":2022,"cardBillsTotal":[{"serial":"941368618045","value":2.2884,"vat":0.5492,"total":2.8376,"energy":0}]},{"firstName":"Θωμάς","lastName":"Παπαϊωάννου","
CodePudding user response:
You should iterate your http reponse, because it is a list and at first you should access each element. So instead of these,
cardBills = jsonDecode(response.body)["results"]["cardBillsTotal"] as List;
//cardBillsTotal = jsonDecode(response.body)["cardBillsTotal"] as List;
month = jsonDecode(response.body)['month'];
userid = jsonDecode(response.body)['userID'];
you should have something like this:
final response = jsonDecode(response.body)["results"];
(response as List<dynamic>).forEach((e) {
month = e['month'];
userid = e['userID'];
//and so on
})
.toList()
I think you get the main idea.