Flutter错误:RangeError(索引):无效值:不在0..2范围内(包括3) [英] Flutter Error: RangeError (index): Invalid value: Not in range 0..2, inclusive: 3

查看:2759
本文介绍了Flutter错误:RangeError(索引):无效值:不在0..2范围内(包括3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在扑朔迷离中使用了一长串.所有项目都可以正常渲染,但还会出现以下错误:

I am using a long list in flutter. All the items are rendering fine but also following error :

RangeError (index): Invalid value: Not in range 0..2, inclusive: 3

以下是我的代码:

@override
Widget build(BuildContext context) {
return Container(
  child: getList(),
 );
}

以下是我的getList()方法:

Following is my getList() method :

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()方法:

And following is my getListItem() method:

List<String> getListItems(){
return ["Faizan", "Usman", "Naouman"];
}

以下是错误的屏幕截图:

Following is the screenshot of error :

推荐答案

您应该将itemCount参数传递给ListView.builder,以使其能够了解商品计数

You should pass the itemCount parameter to the ListView.builder to allow it to know the item count

Widget getList() {
  List<String> list = getListItems();
  ListView myList = new ListView.builder(
    itemCount: list.length,
    itemBuilder: (context, index) {
    return new ListTile(
      title: new Text(list[index]),
    );
  });
  return myList;
}

这篇关于Flutter错误:RangeError(索引):无效值:不在0..2范围内(包括3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆