skip to Main Content

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


  1. Chosen as BEST ANSWER

    this my Anime Model

    Anime model.dart:

    class Datum {
      final int malId;
      final String url;
      final Map<String, Image> images;
      final Trailer trailer;
      final bool approved;
      final List<Title> titles;
      final String title;
      final String? titleEnglish;
      final String titleJapanese;
      final List<String> titleSynonyms;
      final DatumType type;
      final Source source;
      final int? episodes;
      final Status status;
      final bool airing;
      final Aired aired;
      final String duration;
      final Rating rating;
      final double score;
      final int scoredBy;
      final int rank;
      final int popularity;
      final int members;
      final int favorites;
      final String synopsis;
      final String? background;
      final Season? season;
      final int? year;
      final Broadcast broadcast;
      final List<Demographic> producers;
      final List<Demographic> licensors;
      final List<Demographic> studios;
      final List<Demographic> genres;
      final List<dynamic> explicitGenres;
      final List<Demographic> themes;
      final List<Demographic> demographics;
    
      Datum({
        required this.malId,
        required this.url,
        required this.images,
        required this.trailer,
        required this.approved,
        required this.titles,
        required this.title,
        this.titleEnglish,
        required this.titleJapanese,
        required this.titleSynonyms,
        required this.type,
        required this.source,
        this.episodes,
        required this.status,
        required this.airing,
        required this.aired,
        required this.duration,
        required this.rating,
        required this.score,
        required this.scoredBy,
        required this.rank,
        required this.popularity,
        required this.members,
        required this.favorites,
        required this.synopsis,
        this.background,
        this.season,
        this.year,
        required this.broadcast,
        required this.producers,
        required this.licensors,
        required this.studios,
        required this.genres,
        required this.explicitGenres,
        required this.themes,
        required this.demographics,
      });
    
      factory Datum.fromJson(String str) => Datum.fromMap(json.decode(str));
    
      String toJson() => json.encode(toMap());
    
      factory Datum.fromMap(Map<String, dynamic> json) => Datum(
            malId: json["mal_id"],
            url: json["url"],
            images: Map.from(json["images"])
                .map((k, v) => MapEntry<String, Image>(k, Image.fromMap(v))),
            trailer: Trailer.fromMap(json["trailer"]),
            approved: json["approved"],
            titles: List<Title>.from(json["titles"].map((x) => Title.fromMap(x))),
            title: json["title"],
            titleEnglish: json["title_english"],
            titleJapanese: json["title_japanese"],
            titleSynonyms: List<String>.from(json["title_synonyms"].map((x) => x)),
            type: datumTypeValues.map[json["type"]]!,
            source: sourceValues.map[json["source"]]!,
            episodes: json["episodes"],
            status: statusValues.map[json["status"]]!,
            airing: json["airing"],
            aired: Aired.fromMap(json["aired"]),
            duration: json["duration"],
            rating: ratingValues.map[json["rating"]]!,
            score: json["score"]?.toDouble(),
            scoredBy: json["scored_by"],
            rank: json["rank"],
            popularity: json["popularity"],
            members: json["members"],
            favorites: json["favorites"],
            synopsis: json["synopsis"],
            background: json["background"],
            season: seasonValues.map[json["season"]]!,
            year: json["year"],
            broadcast: Broadcast.fromMap(json["broadcast"]),
            producers: List<Demographic>.from(
                json["producers"].map((x) => Demographic.fromMap(x))),
            licensors: List<Demographic>.from(
                json["licensors"].map((x) => Demographic.fromMap(x))),
            studios: List<Demographic>.from(
                json["studios"].map((x) => Demographic.fromMap(x))),
            genres: List<Demographic>.from(
                json["genres"].map((x) => Demographic.fromMap(x))),
            explicitGenres:
                List<dynamic>.from(json["explicit_genres"].map((x) => x)),
            themes: List<Demographic>.from(
                json["themes"].map((x) => Demographic.fromMap(x))),
            demographics: List<Demographic>.from(
                json["demographics"].map((x) => Demographic.fromMap(x))),
          );
    
      Map<String, dynamic> toMap() => {
            "mal_id": malId,
            "url": url,
            "images": Map.from(images)
                .map((k, v) => MapEntry<String, dynamic>(k, v.toMap())),
            "trailer": trailer.toMap(),
            "approved": approved,
            "titles": List<dynamic>.from(titles.map((x) => x.toMap())),
            "title": title,
            "title_english": titleEnglish,
            "title_japanese": titleJapanese,
            "title_synonyms": List<dynamic>.from(titleSynonyms.map((x) => x)),
            "type": datumTypeValues.reverse[type],
            "source": sourceValues.reverse[source],
            "episodes": episodes,
            "status": statusValues.reverse[status],
            "airing": airing,
            "aired": aired.toMap(),
            "duration": duration,
            "rating": ratingValues.reverse[rating],
            "score": score,
            "scored_by": scoredBy,
            "rank": rank,
            "popularity": popularity,
            "members": members,
            "favorites": favorites,
            "synopsis": synopsis,
            "background": background,
            "season": seasonValues.reverse[season],
            "year": year,
            "broadcast": broadcast.toMap(),
            "producers": List<dynamic>.from(producers.map((x) => x.toMap())),
            "licensors": List<dynamic>.from(licensors.map((x) => x.toMap())),
            "studios": List<dynamic>.from(studios.map((x) => x.toMap())),
            "genres": List<dynamic>.from(genres.map((x) => x.toMap())),
            "explicit_genres": List<dynamic>.from(explicitGenres.map((x) => x)),
            "themes": List<dynamic>.from(themes.map((x) => x.toMap())),
            "demographics": List<dynamic>.from(demographics.map((x) => x.toMap())),
          };
    }
    

  2. Datum.fromJson expect a string, but you’ve already decoded on result. Therefore use .fromMap where it will accept a map.

    final List<Datum> respoList =
              result.map((e) => Datum.fromMap(e)).toList();
    
    Login or Signup to reply.
  3. 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))

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search