所以我創建了這個小部件,即進行 API 呼叫,然后使用接收到的資料填充 Listview.Builder。出于某種原因,它只顯示前 13 個結果(端點回傳 20 個結果),然后拋出我收到的錯誤。我有確切的小部件布局和 API 呼叫(盡管是不同的端點,但回應是相同的),并且它作業得很好,顯示了所有 20 個結果。我在這里做錯了嗎?當我在 API 呼叫后檢查變數的長度時,它顯示它包含所有 20 個專案,并且我已將每個專案單獨列印到控制臺。現在幾乎被難住了。也幾乎是一個顫動的菜鳥供參考:)
import 'package:flutter/material.dart';
import 'package:testapp/widgets/movie_card.dart';
import 'package:http/http.dart' as http;
import 'dart:convert' as cnv;
class TvShows extends StatefulWidget {
const TvShows({Key? key}) : super(key: key);
@override
_TvShowsState createState() => _TvShowsState();
}
class _TvShowsState extends State<TvShows> {
List tvShows = [];
@override
void initState() {
getPopularShows();
super.initState();
}
@override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(border: Border.all(color: Colors.black)),
height: 400,
child: ListView.builder(
scrollDirection: Axis.horizontal,
shrinkWrap: true,
itemCount: tvShows.length,
itemBuilder: (context, index) {
return MovieCard(
overview: tvShows[index]["overview"],
id: tvShows[index]["id"].toString(),
index: index,
title: tvShows[index]["name"],
rating: tvShows[index]["vote_average"].toString(),
imageUrl: tvShows[index]["poster_path"],
genreId: tvShows[index]["genre_ids"][0].toString(),
);
}),
);
}
void getPopularShows() async {
final params = {
"api_key": "apikey",
"language": "en-US",
"page": "1"
};
Uri url = Uri.https('api.themoviedb.org', '/3/tv/popular', params);
http.Response res = await http.get(url);
var body = cnv.jsonDecode(res.body);
body["results"].forEach((value) {
tvShows.add(value);
});
}
}
uj5u.com熱心網友回復:
我解決了。發現這是一個后端錯誤。與此代碼無關。:)
uj5u.com熱心網友回復:
我是平臺新手,但你在容器中做了一個條件 If(tvShows == 0){ Return container (progressIndicator);
) 否則 { 你的容器在這里 }
uj5u.com熱心網友回復:
Use This
ListView.builder(
scrollDirection: Axis.horizontal,
shrinkWrap: true,
itemCount: tvShows.length,
itemBuilder: (context, index) {
return tvShows.length > 0 ? MovieCard(
overview: tvShows[index]["overview"],
id: tvShows[index]["id"].toString(),
index: index,
title: tvShows[index]["name"],
rating: tvShows[index]["vote_average"].toString(),
imageUrl: tvShows[index]["poster_path"],
genreId: tvShows[index]["genre_ids"][0].toString(),
) : CircularProgressIndicator();
}),
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/398596.html
