Unhandled Exception: InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic>

176,935

Solution 1

Here are 2 common ways this could go wrong:

  1. If your response is a json array like

    [
        {
          key1: value1,
          key2: value2,
          key3: value3,
        },
        {
          key1: value1,
          key2: value2,
          key3: value3,
        },
    
        .....
    ] 
    

    Then, we use data[0]["name"], not data[0].name Unless we cast to an object that has the name property, we cannot use data[0].name

    We cast like this data = json.decode(response.body).cast<ObjectName>();

    ObjectName can be whatever object you want (Inbuilt or Custom). But make sure it has the name property

  2. If your response is a JSON object like

    {
        dataKey: [
          {
            key1: value1,
            key2: value2,
            key3: value3,
          } 
        ] 
    }
    

    Then json.decode will return a Map, not a List

    Map<String, dynamic> map = json.decode(response.body);
    List<dynamic> data = map["dataKey"];
    print(data[0]["name"]);
    

Solution 2

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

Solution 3

As doesn't change the type, it's just an assertion.

You need to use:

map['eventType'].cast<String, dynamic>() or

Map<String, dynamic>.from(map['eventType'])

You can also solved by this way:

Map<String, dynamic> myMap = Map<String, dynamic>.from(/*Your Source*/ );

Solution 4

Easiest way (one dimensional):

Map<String, dynamic> data = new Map<String, dynamic>.from(json.decode(response.body));

print(data['name']);

Solution 5

You are trying to case an Instance of InternalLinkedHashMap which is not possible.

You should Serialize and deserialize it back to Map<String, dynamic>.

InternalLinkedHashMap<String, dynamic> invalidMap;

final validMap =
        json.decode(json.encode(invalidMap)) as Map<String, dynamic>;
Share:
176,935
harunB10
Author by

harunB10

Updated on January 21, 2022

Comments

  • harunB10
    harunB10 over 2 years

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

    Future<String> login() async {
        var response = await http.get(
            Uri.encodeFull("https://etrans.herokuapp.com/test/2"),
            headers: {"Accept": "application/json"});
    
        this.setState(() {
          data = json.decode(response.body);
        });
    
    
        print(data[0].name);
        return "Success!";
      }
    

    Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List

    What could be the reason?

  • AKASH MATHWANI
    AKASH MATHWANI about 4 years
    use response.body instead of snapshot.value
  • Rajesh
    Rajesh almost 4 years
    he used snippet for firebasedatabase
  • Admin
    Admin almost 4 years
    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.
  • Llama
    Llama over 3 years
    give some more details
  • Blanc Chen
    Blanc Chen almost 3 years
    i got the same issue. do you mind to check it? stackoverflow.com/questions/67564553/…
  • Rammehar Sharma
    Rammehar Sharma over 2 years
    and how we convert List<dynamic> to List<Map<String, dynamic>
  • AMAL MOHAN N
    AMAL MOHAN N over 2 years
    @Diyen Momjang, my array is like datakey:[{key:value}, {key:value}, {key:value}] and I'm facing the same issue, can you help me out?
  • AK IJ
    AK IJ over 2 years
    check api response formate. e.x => var decode= json.decoder(response.body)