Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Error: Expected a value of type 'FutureOr<List<dynamic>>', but got one of type '_JsonMap'

Tags:

json

flutter

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);
  }
like image 468
murat murat Avatar asked Oct 15 '25 09:10

murat murat


1 Answers

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.

like image 186
Benyamin Avatar answered Oct 17 '25 00:10

Benyamin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!