Error: type ‘_Map<String, dynamic>’ is not a subtype of type ‘String’
how to solve this error. see my code, Anime Api calling with Getx.
Anime Api : https://api.jikan.moe/v4/anime
anime_api_server.dart
:
Future<List<Datum>> fetchAnimeData() async {
final response =
await http.get(Uri.parse('https://api.jikan.moe/v4/anime'));
if (response.statusCode == 200) {
final List result = jsonDecode(response.body)['data'];
final List<Datum> respoList =
result.map((json) => Datum.fromJson(json)).toList();
return respoList;
} else {
throw Exception('Failed to load anime data');
}
}
anime_controller.dart
:
AnimeServer apiService = AnimeServer();
var isLoading = false.obs;
var animedataList = <Datum>[].obs;
@override
void onInit() {
fetchAnimeData();
super.onInit();
}
void fetchAnimeData() async {
try {
isLoading(true);
final List<Datum> animeList = await apiService.fetchAnimeData();
animedataList.addAll(animeList);
} catch (e) {
print("Error: ${e.toString()}");
} finally {
isLoading(false);
}
}
my debug console:
Restarted application in 5,852ms.
[GETX] Instance "GetMaterialController" has been created
[GETX] Instance "GetMaterialController" has been initialized
[GETX] Instance "AnimeController" has been created
[GETX] Instance "AnimeController" has been initialized
I/flutter (10968): Error: type '_Map<String, dynamic>' is not a subtype of type 'String'
please, guide this program issue fix on the comment.
3
Answers
this my Anime Model
Anime model.dart:
Datum.fromJson
expect a string, but you’ve already decoded onresult
. Therefore use.fromMap
where it will accept a map.I hade the same issue i get response from API as _Map and its not subtype of Map<String, dynaimc>
if you want it as Map<K, V> you can try adding the
response.body
like
Map<String, dynamic> myMap = jsonDecode(jsonEncode(response.body))