Home > OS >  The method '[]' was called on null. Receiver: null Tried calling: []("title")
The method '[]' was called on null. Receiver: null Tried calling: []("title")

Time:11-08

hi I'm trying to pull data from imdb api but I come up to this error but I haven't found any solutions yet I'd be appreciated if you have any answers

how I'm trying to pull data

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"),
              );
            }
          }),
    );

CodePudding user response:

jsonDecoding above api call result returns

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
}

That's why you are getting above error. inside FutureBuilder remove ListViewBuilder. Since snapshot.data is a Map.

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"),
              );
            }
          });
  • Related