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
Widget getListView(){
var itemList = getListElement();
var list = ListView.builder(
itemCount: itemList.length,
itemBuilder:(context, index){
return ListTile(
title: Text(itemList[index]),
);
}
);
return list;
}
it works in small scale but in my case it doesn't work.
[ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: RangeError (index): Invalid value: Not in inclusive range 0..17: 18
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];
},
);
}
},
),
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;
}
I had this problem inside a GridView, but it had nothing to do with my GridView. I was splitting addresses by commas, ex addresses[index].split(',')[0]
, but I came across a address that had no commas which is why I suddenly got this error. Look closely through the debug console to find the exact line of the error, and test every piece of code in your GridView to pinpoint the error.