[Flutter Error]The following RangeError was thrown building KeyedSubtree-[<0>]: RangeError (index): Invalid value: Valid value range is empty: 0

错误:

The following RangeError was thrown building KeyedSubtree-[<0>]:
RangeError (index): Invalid value: Valid value range is empty: 0

原因:

运行时还没获取到数据,就执行了 类似这样的数据输出,
child: Text('${this._newsDataList[index].title}'

解决方法:对数据是否获取到进行判断。

添加 this._newsDataList.length > 0

return this._newsDataList.length > 0
    ? ListView.builder(
    ...
)
    : Container(
        margin: EdgeInsets.all(ScreenAdapter.setHeight(20)),
        height: ScreenAdapter.setHeight(800),
    );
posted @ 2021-01-30 22:45  漫游者杰特  阅读(806)  评论(0编辑  收藏  举报