【问题标题】:Flutter List View error in building list view构建列表视图中的颤振列表视图错误
【发布时间】:2018-10-26 15:15:12
【问题描述】:

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

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

我得到的错误是:

RangeError(index):无效值:有效值范围为空:0

这是列表视图构建器:

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

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

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

    尝试添加

    new ListView(
       ...
       itemCount: _movie.length
    

    【讨论】:

    • 这确实消除了错误,但是没有显示任何内容
    • 因为您没有加载任何内容。您可能需要致电setState
    • 动态项目列表怎么样?
    • 我面临同样的问题,我确实提到了 itemcount,但由于某种原因它只显示前 20 个元素
    • 很好的解释。与您的代码相似的代码中有很多答案。但是在无效索引处访问集合而不是文档的想法为我解决了这个问题。谢谢。
    【解决方案2】:

    您需要添加项目计数

    new ListView(
       ...
       itemCount: _movie.length
    

    没有它ListView.Builder不知道列表的位置,我们不会根据位置显示列表

    【讨论】:

    • 我面临同样的问题,我确实提到了 itemcount,但由于某种原因它只显示前 20 个元素
    【解决方案3】:

    尝试添加listview.builder的索引, Listview.builder 用于非常长或未知的列表集,您所做的正确。 只需计算您从 API 响应中获得的值的数量,然后将索引添加为参数。

    如需更多参考,请查看以下网址: https://docs.flutter.io/flutter/widgets/ListView/ListView.builder.html

    【讨论】:

      猜你喜欢
      • 2020-06-19
      • 2019-04-08
      • 2021-09-28
      • 1970-01-01
      • 1970-01-01
      • 2018-04-26
      • 2019-04-25
      • 2018-12-06
      • 1970-01-01
      相关资源
      最近更新 更多