'Error on trying to jsonDecode String in Flutter application
When I try to decode a json String: [{"device-mac":"C9BF2EB47C17","on":true}]
with the following function the error:
I/flutter ( 8954): type 'List' is not a subtype of type 'String'
get's thrown.
My function is this one, the json String is loaded from a Rest API endpoint:
Future<void> _getDevices() async {
var parsedList;
try {
final response = await restApiClient.get(
'/getDevices',
);
List<dynamic> parsedListJson = jsonDecode(response.data);
List<Device> itemsList =
List<Device>.from(parsedListJson.map((i) => Device.fromJson(i)));
}
}
Solution 1:[1]
In the end the reason for it not working was that my REST endpoint already returned a list JSON Type which made the function "jsonDecode" redundant.
Solution 2:[2]
I think it can parse Sting only and you passed a List of maps, you should either make a loop and then cast the map to string using 'response.data.toString' and then add the value to the list or if it's always one value try using 'response.data.first.toString()
Solution 3:[3]
I think it can parse Sting only and if you passed a List of maps, you should either make a loop and then cast the map to string using 'response.data.torturing and then add the value to the list or if it's always one value try using 'response.data.first.toString()
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 | Thilo Jaeggi |
Solution 2 | LMech |
Solution 3 | Rahat |