When I try to decode JSON I get this error:
Expected a value of type 'FutureOr<List>', but got one of type '_JsonMap'
This is my code for getting the data from the API:
Future<List> getData() async {
final prefs = await SharedPreferences.getInstance();
final key = 'token';
final value = prefs.get(key) ?? 0;
String myUrl = "$serverUrl/userchecks";
http.Response response = await http.get(Uri.parse(myUrl), headers: {
'Accept': 'application/json',
'Authorization': 'Bearer $value'
});
print('Response status : ${response.statusCode}');
return json.decode(response.body);
}
your returned value from JSON is not a List but it is a JSON map, so you have to change the Future return type like this:
Future<Map<String,dynamic>> getData() async {
final prefs = await SharedPreferences.getInstance();
final key = 'token';
final value = prefs.get(key) ?? 0;
String myUrl = "$serverUrl/userchecks";
http.Response response = await http.get(Uri.parse(myUrl), headers: {
'Accept': 'application/json',
'Authorization': 'Bearer $value'
});
print('Response status : ${response.statusCode}');
return json.decode(response.body);
}
or you can do it as it is answered here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With