Unhandled Exception: InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic>
Solution 1
Here are 2 common ways this could go wrong:
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"]
, notdata[0].name
Unless we cast to an object that has the name property, we cannot usedata[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 propertyIf your response is a JSON object like
{ dataKey: [ { key1: value1, key2: value2, key3: value3, } ] }
Then
json.decode
will return a Map, not a ListMap<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>;
harunB10
Updated on January 21, 2022Comments
-
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 about 4 yearsuse response.body instead of snapshot.value
-
Rajesh almost 4 yearshe used snippet for firebasedatabase
-
Admin almost 4 yearsThis 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 over 3 yearsgive some more details
-
Blanc Chen almost 3 yearsi got the same issue. do you mind to check it? stackoverflow.com/questions/67564553/…
-
Rammehar Sharma over 2 yearsand how we convert List<dynamic> to List<Map<String, dynamic>
-
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 over 2 yearscheck api response formate. e.x => var decode= json.decoder(response.body)