Home > Software engineering >  Flutter Range Error: Invalid Value JSON call
Flutter Range Error: Invalid Value JSON call

Time:05-04

When reading a JSON like this:

{
  "altitude": 7, 
  "eoi_code": "08015021", 
  "last_calculated_at": "Sat, 30 Apr 2022 19:13:37 GMT", 
  "latitude": 41.443985, 
  "longitude": 2.2378986, 
  "name": "Badalona", 
  "pollutants": [], 
  "pollution": 0.24150975081597828, 
  "station_type": "background", 
  "urban_area": "urban"
}

There is an error: [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'FutureOr<List>' The JSON is not a Map. Here I show part of the code:

Future<List> getStation(String id) async {
    final String url2 = url   id;
    final response = await http.get(Uri.parse(url2));

    if (response.statusCode == 200) {
      return json.decode(response.body);
    }

    return [];
  }

The call:

  void getStation() async {
    List tmp = await sj.getStation(widget.id);
    if (mounted) {
      setState(() {
        station = tmp;
      });
    }
  }

CodePudding user response:

JSON is a map not a list.

  Future<Map<String, dynamic>> getStation(String id) async {
    final String url2 = url   id;
    final response = await http.get(Uri.parse(url2));

    if (response.statusCode == 200) {
      return json.decode(response.body);
    }

    return {};
  }
  • Related