gpt4 book ai didi

dart - Flutter List View 构建 ListView 报错

转载 作者:IT王子 更新时间:2023-10-29 06:46:34 25 4
gpt4 key购买 nike

我在构建 ListView 时遇到错误,起初我以为 API 没有返回任何内容,但我在 flutter 中打印了 movies 变量,它控制台记录了这些值。顺便说一句,我正在尝试重新创建这个项目:

https://github.com/mlabouardy/flutter-watchnow

我得到的错误是:

RangeError (index): Invalid value: Valid value range is empty: 0

这是 ListView 生成器:

class TopMoviesState extends State<TopMovies> {
List<Movie> _movies = new List();
final _apiGatewayURL = "https://gfioehu47k.execute-api.us-west-1.amazonaws.com/staging/main";

Widget _fetchMovies() {
return new ListView.builder(
padding: const EdgeInsets.all(16.0),
itemBuilder: (context, i){
return new Card(
child: new Container(
height: 250.0,
child: new Padding(
padding: new EdgeInsets.all(2.0),
child: new Row(
children: <Widget>[
new Align(
child: new Hero(
child: new Image.network("https://image.tmdb.org/t/p/w500"+this._movies[i].getPoster()),
tag: this._movies[i].getTitle()
),
alignment: Alignment.center,
),
new Expanded(
child: new Stack(
children: <Widget>[
new Align(
child: new Text(
this._movies[i].getTitle(),
style: new TextStyle(fontSize: 11.0, fontWeight: FontWeight.bold),
),
alignment: Alignment.topCenter,
),
new Align(
child: new Padding(
padding: new EdgeInsets.all(4.0),
child: new Text(
this._movies[i].getOverview(),
maxLines: 8,
overflow: TextOverflow.ellipsis,
style: new TextStyle(fontSize: 12.0, fontStyle: FontStyle.italic)
)
),
alignment: Alignment.centerRight,
),
new Align(
child: new Text(
this._movies[i].getReleaseDate(),
style: new TextStyle(fontSize: 11.0, fontWeight: FontWeight.bold),
),
alignment: Alignment.bottomRight,
),
]
)
)
]
)
)
)
);
}
);
}

这就是我运行从 API 获取的每个值的方式:

void _addMovie(dynamic movie){
this._movies.add(new Movie(
title: movie["title"],
overview: movie["overview"],
poster: movie["poster_path"],
releaseDate: movie["release_date"]
));
}
@override
void initState() {
super.initState();
http.get(this._apiGatewayURL)
.then((response) => response.body)
.then(json.decode)
.then((movies) {
movies.forEach(_addMovie);
});
}

最佳答案

您没有为 ListView 指定 itemCount。因此,如果您有足够的空间来显示比可用项目数量更多的内容,您将尝试在无效索引处访问 _movies。导致这个错误。

尝试添加

new ListView(
...
itemCount: _movie.length

关于dart - Flutter List View 构建 ListView 报错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50373040/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com