Search code examples
flutterdartthemoviedb-api

Type int is not subtype of type iterable<dynamic> tmdb api


I'm getting an error while get the data by their genres id

The code I'm fetching the data from below, I'm passing int genre id to the fetch method I tried to pass the id in the Postman and getting no errors, using tmdb API https://api.themoviedb.org/3/discover/movie?with_genres=36 the response of Postman is below:

I'm getting an error while get the data by their genres id

import 'dart:convert';

import 'package:bloc/bloc.dart';
import 'package:http/http.dart' as http;
import 'package:meta/meta.dart';
import 'package:movies/components/config/api_config.dart';
import 'package:movies/views/browse/manger/models/movies_genres_model.dart';

part 'movies_genres_state.dart';

class MoviesGenresCubit extends Cubit<MoviesGenresState> {
MoviesGenresCubit() : super(MoviesGenresInitial());

List<Results> movies = [];

Future<void> fetchMoviesByGenres(int categoryId) async {
 emit(MoviesGenresLoading());
 try {
   Uri url = Uri.https(ApiConfig.BASE_URL, "/3/discover/movie", {
     'with_genres': categoryId,
     'sort_by': 'popularity.desc',
   });
   http.Response resposne = await http.get(url, headers: {
     "Authorization": ApiConfig.tokenKey,
   });
   Map<String, dynamic> json = jsonDecode(resposne.body);

   MoviesGenres moviesGenres = MoviesGenres.fromJson(json);

   movies = moviesGenres.results ?? [];

   emit(MoviesGenresSuccess());
 } catch (e) {
   emit(MoviesGenresFailure(e.toString()));
 }
}
}

The response from post:

{
"page": 1,
"results": [
    {
        "adult": false,
        "backdrop_path": "/nb3xI8XI3w4pMVZ38VijbsyBqP4.jpg",
        "genre_ids": [
            18,
            36
        ],
        "id": 872585,
        "original_language": "en",
        "original_title": "Oppenheimer",
        "overview": "The story of J. Robert Oppenheimer's role in the development of the atomic bomb during World War II.",
        "popularity": 398.697,
        "poster_path": "/8Gxv8gSFCU0XGDykEGv7zR1n2ua.jpg",
        "release_date": "2023-07-19",
        "title": "Oppenheimer",
        "video": false,
        "vote_average": 8.1,
        "vote_count": 7397
    },
    {
        "adult": false,
        "backdrop_path": "/1ZSKH5GGFlM8M32K34GMdaNS2Ew.jpg",
        "genre_ids": [
            10402,
            36,
            18
        ],
        "id": 802219,
        "original_language": "en",
        "original_title": "Bob Marley: One Love",
        "overview": "Jamaican singer-songwriter Bob Marley overcomes adversity to become the most famous reggae musician in the world.",
        "popularity": 365.434,
        "poster_path": "/4eWeXswkAUIvdVWFvPrUFu2TxuI.jpg",
        "release_date": "2024-02-14",
        "title": "Bob Marley: One Love",
        "video": false,
        "vote_average": 7.065,
        "vote_count": 397
    },
    {
        "adult": false,
        "backdrop_path": "/eHMh7kChaNeD4VTdMCXLJbRTzcI.jpg",
        "genre_ids": [
            18,
            36,
            10752
        ],
        "id": 753342,
        "original_language": "en",
        "original_title": "Napoleon",
        "overview": "An epic that details the checkered rise and fall of French Emperor Napoleon Bonaparte and his relentless journey to power through the prism of his addictive, volatile relationship with his wife, Josephine.",
        "popularity": 234.926,
        "poster_path": "/vcZWJGvB5xydWuUO1vaTLI82tGi.jpg",
        "release_date": "2023-11-22",
        "title": "Napoleon",
        "video": false,
        "vote_average": 6.478,
        "vote_count": 1914
    },
    {
        "adult": false,
        "backdrop_path": "/acvE3RWjDLgvbL2RtcyzkrsAyNV.jpg",
        "genre_ids": [
            80,
            36,
            18
        ],
        "id": 466420,
        "original_language": "en",
        "original_title": "Killers of the Flower Moon",
        "overview": "When oil is discovered in 1920s Oklahoma under Osage Nation land, the Osage people are murdered one by one—until the FBI steps in to unravel the mystery.",
        "popularity": 159.845,
        "poster_path": "/dB6Krk806zeqd0YNp2ngQ9zXteH.jpg",
        "release_date": "2023-10-18",
        "title": "Killers of the Flower Moon",
        "video": false,
        "vote_average": 7.498,
        "vote_count": 2678
    },
    {
        "adult": false,
        "backdrop_path": "/rI1u4JgtnAZC99wXX4gwro4yaxe.jpg",
        "genre_ids": [
            18,
            36
        ],
        "id": 6844,
        "original_language": "en",
        "original_title": "The Ten Commandments",
        "overview": "Escaping death, a Hebrew infant is raised in a royal household to become a prince. Upon discovery of his true heritage, Moses embarks on a personal quest to reclaim his destiny as the leader and liberator of the Hebrew people.",
        "popularity": 134.979,
        "poster_path": "/2rBUaC6ngeFmYXjL4KA3eLjt5wA.jpg",
        "release_date": "1956-10-05",
        "title": "The Ten Commandments",
        "video": false,
        "vote_average": 7.757,
        "vote_count": 1488
    },
    {
        "adult": false,
        "backdrop_path": "/yQIBS8B9l2qXoPoPtxSXvH7CfoT.jpg",
        "genre_ids": [
            18,
            36,
            10752
        ],
        "id": 324786,
        "original_language": "en",
        "original_title": "Hacksaw Ridge",
        "overview": "WWII American Army Medic Desmond T. Doss, who served during the Battle of Okinawa, refuses to kill people and becomes the first Conscientious Objector in American history to receive the Congressional Medal of Honor.",
        "popularity": 118.705,
        "poster_path": "/wuz8TjCIWR2EVVMuEfBnQ1vuGS3.jpg",
        "release_date": "2016-10-07",
        "title": "Hacksaw Ridge",
        "video": false,
        "vote_average": 8.193,
        "vote_count": 13160
    },
    {
        "adult": false,
        "backdrop_path": "/vdpE5pjJVql5aD6pnzRqlFmgxXf.jpg",
        "genre_ids": [
            18,
            36
        ],
        "id": 906126,
        "original_language": "es",
        "original_title": "La sociedad de la nieve",
        "overview": "On October 13, 1972, Uruguayan Air Force Flight 571, chartered to take a rugby team to Chile, crashes into a glacier in the heart of the Andes.",
        "popularity": 123.683,
        "poster_path": "/2e853FDVSIso600RqAMunPxiZjq.jpg",
        "release_date": "2023-12-15",
        "title": "Society of the Snow",
        "video": false,
        "vote_average": 8.069,
        "vote_count": 2349
    },
    {
        "adult": false,
        "backdrop_path": "/z5AUKpMcvF7Grw7JDTgoNzOU6LD.jpg",
        "genre_ids": [
            18,
            36
        ],
        "id": 850165,
        "original_language": "en",
        "original_title": "The Iron Claw",
        "overview": "The true story of the inseparable Von Erich brothers, who made history in the intensely competitive world of professional wrestling in the early 1980s. Through tragedy and triumph, under the shadow of their domineering father and coach, the brothers seek larger-than-life immortality on the biggest stage in sports.",
        "popularity": 132.989,
        "poster_path": "/nfs7DCYhgrEIgxKYbITHTzKsggf.jpg",
        "release_date": "2023-12-21",
        "title": "The Iron Claw",
        "video": false,
        "vote_average": 7.58,
        "vote_count": 389
    },
    {
        "adult": false,
        "backdrop_path": "/aPQsU3yLDUOhLJYnSqkhKRkQTAw.jpg",
        "genre_ids": [
            36,
            18,
            10752
        ],
        "id": 467244,
        "original_language": "en",
        "original_title": "The Zone of Interest",
        "overview": "The commandant of Auschwitz, Rudolf Höss, and his wife Hedwig, strive to build a dream life for their family in a house and garden next to the camp.",
        "popularity": 108.461,
        "poster_path": "/hUu9zyZmDd8VZegKi1iK1Vk0RYS.jpg",
        "release_date": "2023-12-15",
        "title": "The Zone of Interest",
        "video": false,
        "vote_average": 7.318,
        "vote_count": 952
    },
    {
        "adult": false,
        "backdrop_path": "/4dcx4odNyL7IQ8c6vb6oYkjk9rg.jpg",
        "genre_ids": [
            36,
            18
        ],
        "id": 798612,
        "original_language": "en",
        "original_title": "Shirley",
        "overview": "Shirley Chisholm makes a trailblazing run for the 1972 Democratic presidential nomination after becoming the first Black woman elected to Congress.",
        "popularity": 116.363,
        "poster_path": "/ooo4D4kyZyFee5aiNfwZ43oKOkc.jpg",
        "release_date": "2024-03-21",
        "title": "Shirley",
        "video": false,
        "vote_average": 7.467,
        "vote_count": 15
    },
    {
        "adult": false,
        "backdrop_path": "/7grv5ULlK9wMr2T1fkgr56HlbT.jpg",
        "genre_ids": [
            18,
            36,
            10752
        ],
        "id": 760774,
        "original_language": "en",
        "original_title": "One Life",
        "overview": "British stockbroker Nicholas Winton visits Czechoslovakia in the 1930s and forms plans to assist in the rescue of Jewish children before the onset of World War II, in an operation that came to be known as the Kindertransport.",
        "popularity": 112.333,
        "poster_path": "/yvnIWt2j8VnDgwKJE2VMiFMa2Qo.jpg",
        "release_date": "2023-09-09",
        "title": "One Life",
        "video": false,
        "vote_average": 7.696,
        "vote_count": 240
    },
    {
        "adult": false,
        "backdrop_path": "/g7Fr6Pd1bR9G5Y9E47h7pmYTcX4.jpg",
        "genre_ids": [
            36,
            28,
            18
        ],
        "id": 960481,
        "original_language": "mn",
        "original_title": "Хотулун гүнж: Алтан судрын эрэлд",
        "overview": "Princess Khotulun is the daughter of Haidu Khan of the Ugudei dynasty. The film tells the story of Princess Khotulun, the son of Kublai Khan, known in Western and Eastern history as a wrestling princess, and her struggle to reclaim her Golden Sutra.",
        "popularity": 86.897,
        "poster_path": "/gmfPD15AR0FXyNTQE19TbuJNrwu.jpg",
        "release_date": "2021-12-03",
        "title": "The Warrior Princess",
        "video": false,
        "vote_average": 7.2,
        "vote_count": 59
    },
      
}

Solution

  • From TMDB API documentation, this issue likely occur from how you're passing the categoryId to the Uri.https

    with_genres parameter expects a string but you passing a int

      // Convert the categoryId to a String
    String genreIdAsString = categoryId.toString();
    
    
    Uri url = Uri.https(ApiConfig.BASE_URL, "/3/discover/movie", {
      'with_genres': genreIdAsString,
      'sort_by': 'popularity.desc',
    
    });