我在 Flutter 中使用了很长的列表。所有项目都呈现正常,但我也收到以下错误:
RangeError (index): Invalid value: Not in range 0..2, inclusive: 3
以下是我的代码:
@override
Widget build(BuildContext context) {
return Container(
child: getList(),
);
}
以下是我的 getList()
方法:
Widget getList (){
List<String> list = getListItems();
ListView myList = new ListView.builder(itemBuilder: (context, index){
return new ListTile(
title: new Text(list[index]),
);
});
return myList;
}
以下是我的 getListItem()
方法:
List<String> getListItems(){
return ["Faizan", "Usman", "Naouman"];
}
以下是错误截图:
原文由 Muhammad Faizan 发布,翻译遵循 CC BY-SA 4.0 许可协议
您应该将
itemCount
参数传递给ListView.builder
以使其知道项目数