I am currently building an app to read data through an API and I am trying to parse a JSON API from JSON Placeholder.
This is the function in main.dart to read the JSON into the widget:-
Future fetchData() async {
final response = await http.get(Uri.parse('http://127.0.0.1:5000/api/data'));
if (response.statusCode == 200) {
final Map<String, dynamic> responseBody = json.decode(response.body);
print(responseBody);
final List<dynamic> apiData = responseBody['data'];
for (var item in apiData) {
final int setNumber = item['setNumber'];
final int subNumber = item['subNumber'];
final String dateTime = item['dateTime'];
.
.
.
}
}
else {
print('Failed to load data');
}}
This is the JSON information i’m trying to read although only some part of it:-
data={"id": 12345,
"number": 67890,
"data": [
{
'setNumber': 123456,
'subNumber': 0,
'deptCode': 1,
'dateTime': "2024-07-22 09:49:27"
},
{
'setNumber': 654321,
'subNumber': 0,
'deptCode': 2,
'dateTime': "2024-07-22 09:49:27"
},
]
}
The error i am currently running into is:
Unhandled Exception: type ‘_Map<String, dynamic>’ is not a subtype of type ‘List’
How do i rectify this error?
Parag is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.