I have an AchievementResponse
model with a fromJson
method. I receive data from the server and I want to place this data in a list of type AchievementResponse
. But when I get the data and add it to the list, I get the error _TypeError (type '_Map<String, dynamic>' is not a subtype of type 'String')
. If I add the toString()
method, I also get an error (screenshots below). Tell me how to fix the error?
model
factory AchievementResponse.fromJson(Map<String, dynamic> json) {
return AchievementResponse(
id: json['id'],
title: json['title'],
description: json['description'],
status: json['status'],
);
}
response
Future<List<AchievementResponse>?> getInfo
...
if (response is dio.Response) {
final data = response.data;
print(data['achievementsProgress']);
return jsonDecode(data['achievementsProgress'].toString())
.map(AchievementResponse.fromJson)
.toList();
error
error withou toString() method
3
Answers
Change code of return
As you pay attention to your error, you will see the data that you are getting from your server is
Map<String, dynamic>
but you are decoding the response asString
. anyway to fix this problem you can passachievementsProgress
map tofromJson
witout any converting toString
:happy coding…
You don’t need to use
jsonDecode
ondata['achievementsProgress']
, it’s already decoded.Looking at the exception, it seems
data['achievementsProgress']
is a map with adata
list in it. Try this: