方法 '[]' 在 null 上调用。接收方:空 已尝试呼叫:[]( "title" )



嗨,我正试图从imdb api中提取数据,但我遇到了这个错误,但我还没有找到任何解决方案,如果你有任何答案,我将不胜感激

我如何尝试拉数据

Future getMoviesData() async {
    var uri = Uri.https(
        'imdb8.p.rapidapi.com', '/title/get-details', {"tconst": 'tt0944947'});
    var response = await http.get(uri, headers: {
      'X-RapidAPI-Key': '4c475a8679mshd659a75041ce1aap115573jsn8903b41a82cf',
      'X-RapidAPI-Host': 'imdb8.p.rapidapi.com'
    });
    print(response.statusCode);
    print(response.body);
    String data = response.body;
    var output = jsonDecode(data);
    return output;
  }
return Scaffold(
      body: FutureBuilder(
          future: getMoviesData(),
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              return ListView.builder(
                  itemCount: snapshot.data.length,
                  itemBuilder: (context, index) {
                    var imdb = snapshot.data[index];
                    return Column(
                      children: [
                        Text(imdb["title"]),
                      ],
                    );
                  });
            } else {
              return Center(
                child: Text("veri gelmiyor"),
              );
            }
          }),
    );

jsonDecoding以上api调用结果返回

Map<String, dynamic> 

not a List

{
    "@type": "imdb.api.title.title",
    "id": "/title/tt0944947/",
    "image": {
        "height": 1500,
        "id": "/title/tt0944947/images/rm4204167425",
        "url": "https://m.media-amazon.com/images/M/MV5BYTRiNDQwYzAtMzVlZS00NTI5LWJjYjUtMzkwNTUzMWMxZTllXkEyXkFqcGdeQXVyNDIzMzcwNjc@._V1_.jpg",
        "width": 1102
    },
    "runningTimeInMinutes": 57,
    "nextEpisode": "/title/tt1480055/",
    "numberOfEpisodes": 73,
    "seriesEndYear": 2019,
    "seriesStartYear": 2011,
    "title": "Game of Thrones",
    "titleType": "tvSeries",
    "year": 2011
}

这就是为什么你得到高于误差。在FutureBuilder中取出ListViewBuilder。因为snapshot.dataMap

FutureBuilder(
          future: getMoviesData(),
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              return Center(
                child: Column(
                  children: [
                    Text(snapshot.data["title"]),
                  ],
                ),
              );
            } else {
              return const Center(
                child: Text("veri gelmiyor"),
              );
            }
          });

相关内容

  • 没有找到相关文章

最新更新