Unhandled Exception: InternalLinkedHashMap' is not a subtype of type 'List

前端 未结 9 732
夕颜
夕颜 2020-12-15 16:04

I am trying to get the JSON response from the server and output it to the console.

Future login() async {
    var response = await http.get(
            


        
相关标签:
9条回答
  • 2020-12-15 16:22

    You can use new Map<String, dynamic>.from(snapshot.value);

    Works perfect for me.

    0 讨论(0)
  • 2020-12-15 16:23

    You have to convert the runtimeType of data from _InternalLinkedHashMap to an actual List.

    One way is to use the List.from.

    final _data = List<dynamic>.from(
      data.map<dynamic>(
        (dynamic item) => item,
      ),
    );
    
    0 讨论(0)
  • 2020-12-15 16:23

    You can get this error if you are using retrofit.dart and declare the wrong return type for your annotated methods:

    @GET("/search")
    Future<List<SearchResults>> getResults(); 
    // wrong! search results contains a List but the actual type returned by that endpoint is SearchResults 
    

    vs

    @GET("/search")
    Future<SearchResults> getResults(); 
    // correct for this endpoint - SearchResults is a composite with field for the list of the actual results
    
    0 讨论(0)
  • 2020-12-15 16:27

    If you need work with generic fields has a workaround:

    class DicData
    {
      int tot;
      List<Map<String, dynamic>> fields;
    
      DicData({
        this.tot,
        this.fields
      });
    
     factory DicData.fromJson(Map<String, dynamic> parsedJson) {
        return DicData(
            tot: parsedJson['tot'],
            //The magic....
            fields : parsedJson["fields"] = (parsedJson['fields'] as List)
                ?.map((e) => e == null ? null : Map<String, dynamic>.from(e))
                ?.toList()
        );
      }
    
    }
    
    0 讨论(0)
  • 2020-12-15 16:29

    This worked for me:

    1. Create a List Data
    2. Use Map to decode the JSON file
    3. Use the List object Data to fetch the name of the JSON files
    4. With the help of index and the list object I have printed the items dynamically from the JSON file
    setState(){
    
        Map<String, dynamic> map = json.decode(response.body);
        Data  = map["name"];
    }
    
    // for printing
    Data[index]['name1'].toString(),
    
    0 讨论(0)
  • 2020-12-15 16:31

    I had the same error using json_annotation, json_serializable, build_runner. It occurs when calling the ClassName.fromJson() method for a class that had a class property (example: class User has a property class Address).

    As a solution, I modified the generated *.g.dart files of each class, by changing Map<String, dynamic>) to Map<dynamic, dynamic>) in everywhere there is a deep conversion inside the method _$*FromJson

    The only problem is that you have to change it again every time you regenerate your files.

    0 讨论(0)
提交回复
热议问题