'Exception has occurred. _TypeError (type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic>')
I am new to flutter, so I honestly don't know the why I am receiving this error.
List < BaseResponse > pEShippingDetail = [];
Future loadMessageList() async {
http.Response response = await http.get('http://www.efwebservice.com/Public/api/ShipmentItems/GetShippingDetails?token=xxxxx&projectid=xxxxx');
//await Future.delayed(Duration(seconds: 3));
String content = response.body;
List collection = json.decode(content);
List < BaseResponse > _messages = collection.map((json) => BaseResponse.fromJson(json)).toList();
setState(() {
pEShippingDetail = _messages;
});
}
void initState() {
loadMessageList();
super.initState();
}
Solution 1:[1]
Your json.decode(content);
produced a _InternalLinkedHashMap<String, dynamic>
but you are try to assign it to a List
of type dynamic
(default type).
To solve this, first see what is the data format of the response is. Then use required type. You could use final
without static type but that's not the best option here. Map<String, dynamic>
should work but cannot explicitly say it is correct without looking at the json response.
Solution 2:[2]
That get request is returning a JSON object, not a list. JSON objects are decoded to Map<String, dynamic>
. So the exception is because you are trying to assign a Map<String, dynamic>
to a List variable, which cannot be done.
Looking at the response above though, perhaps you mean to use the categories
property in the response, not the entire response?
Have a closer look at the response, and decide which parts of the data you are actually interested in.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | MichaelM |