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

805

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.

Share:
805
murat murat
Author by

murat murat

Updated on January 01, 2023

Comments

  • murat murat
    murat murat over 1 year

    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);
      }