I'm trying to list information from an API remaining on the screen. I have this method:
late List<MyModel> _listAll; // original list fetched from API
late List<MyModel> _displayList;
.
.
void _ListTransaction() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String token = prefs.getString("userToken") ?? "";
dynamic data = await http.get(Uri.parse('....'), headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': '$token',
});
List<MyModel> listAll= [];
for (var u in data) {
MyModel myModel = MyModel.fromJson(u);
print(myModel);
listAll.add(myModel);
}
setState(() {
_listAll = listAll;
_displayList = _listAll ;
});
}
Here I get the error:
_TypeError (type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Iterable')
I also tried this approach:
void _ListTransaction() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String token = prefs.getString("userToken") ?? "";
dynamic data = await http.get(Uri.parse('....'), headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': '$token',
});
var list = json.decode(data.body);
print(list);
setState(() {
_listAll = list;
_displayedList = _petrolList;
});
}
But here I get the error:
_TypeError (type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List')
You guys could help me with this error. I appreciate any comments!
UPDATE I used this approach too, but to no avail!
Here is the structure of my json.decode(data.body);

MyModel:
class TransactionModel {
int? month;
int? year;
double? balanceFull;
double? balanceMonth;
ExpenseModel? expenseModel;
RevenueModel? revenueModel;
TransactionModel();
Transactions() {
month = 00;
year = 0000;
balanceFull = 0;
balanceMonth = 0;
}
TransactionModel.fromJson(Map<String, dynamic> json) {
month = json['month'];
year = json['year'];
balanceFull = json['balanceFull'];
balanceMonth = json['balanceMonth'];
expenseModel = json['expenseModel'];
revenueModel = json['revenueModel'];
}