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

Viewed 124208

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

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

The following is my code:

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

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

And the following is my getListItem() method:

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

the following is the screenshot of error:

enter image description here

9 Answers

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

This error occurs when you run out of values when iterating over an array. In the case of the ListView component missing the itemCount prop, the component attempts to continue to iterate but is unaware when to complete so it eventually continues on out of range (the length of the array).

You could also see this error after running a poorly set up for loop. For example:

var arr = [1, 2, 3];

for (var i=0; i < 4; i++) {
    print(arr[i]);
}

This dart code would result in a range error as well. The array has 3 items yet we attempt to iterate 4 times.

Add the attribute

    itemCount: list.length,

in the listview.builder or gridview.builder.

It works for me on this error:

RangeError (index): Invalid value: Not in inclusive range 0..29: 55

If you are using StreamBuilder then you must use this line of code:

  StreamBuilder(
                  stream: FirebaseFirestore.instance.collection("Tooth")
                  .orderBy("date", descending: false).snapshots() ,
                
                  builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot){
                 
                    if(snapshot.hasData)
                    {
                      return ListView.builder(
                        itemCount: snapshot.data.docs.length,
                          padding: const EdgeInsets.only( top: 20.0),
                        itemBuilder: (BuildContext context, int index) {
                           DocumentSnapshot ds = snapshot.data.docs[index];
 },
                      );
                    }
                   
                  },
                ),

In my case, my itemCount:list.lenght parameter was fine but somewhere in my listTile, I used wrong parameter for list.indexWhere() function .When I fix function, error was gone.

if you use substring and if did not make condition properly then you will get an error

This mainly occurs when you are using wrong index value to fetch data from list. in my case, I was doing the same mistake.

Listview.Builder contains itemcount attribute

Listview.Builder(

itemCount : list.length,

itemBuilde:(context, index)=>

          );
Widget getListView(){
  var itemList = getListElement();
   var list = ListView.builder(
     itemCount: itemList.length,
       itemBuilder:(context, index){
         return ListTile(
           title: Text(itemList[index]),   
         );
         }
   );
   return list;
}
Related