我得到一个错误,而得到的数据由他们的类型id

我从下面获取数据的代码, 我把int genre id传递给fetch方法 我试着在postman 那里传递身份证,没有出错, 使用tmdb API https://api.themoviedb.org/3/discover/movie?with_genres=36 postman 的回应如下:

我得到一个错误,而得到的数据由他们的类型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()));
 }
}
}

来自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
    },
      
}

推荐答案

从TMDBAPI文档中,这个问题可能发生在如何将categoryId传递给Uri.https

with_generates参数需要一个字符串,但您传递了一个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',

});

Flutter相关问答推荐

创建多个具有适当填充和对齐的按钮的最佳方法

Flutter在创建时为提供者调用初始化函数

创建后检索FiRestore文档ID

如何在凌晨12点显示00:00(24小时格式)而不是在Flutter 中显示24:00

为什么Riverpod生成器在这种情况下不生成AsyncNotiator?

Flutter 图标记移动

分页控制器在到达页面末尾之前或在初始化页面时对每个索引调用API

为什么用户界面不会在Flight中的复选框中更改?

Flutter 需要 Xcode 14 或更高版本

使用 Flutter 显示/隐藏带有下拉抽屉动画的小部件

如何在flutter中实现这样的底部导航栏?

获取通过列表呈现的相同 TextEditingController 的值

无法在 ListView (Flutter) 中限制 Card 的最大宽度

这是使用导航功能处理从一页到另一页的移动的最佳方式

Flutter - 根据从第一个 DropdownButtonForm 中 Select 的内容在第二个 DropdownButton 上显示选项

graphql_flutter 错误:非抽象类GraphQLWebSocketChannel缺少实现

在提供程序包更改通知程序中构建倒计时时钟

如何从 Flutter Slider Widget 中移除发光效果?

Flutter 动画页面计数器文本

使用 mocktail 包进行单元测试时,类型Null不是Future类型的子类型