Error:
RangeError (index): Invalid value: Not in inclusive range 0..114: 115
I am using ListView
where when I am using simple index
my code runs fine but when I put index 1
so that I can start with 1 I get the error.
My code is something like this:
Code:
class Main_Index_Page extends StatelessWidget {
Main_Index_Page({Key? key}) : super(key: key);
final items = List<String>.generate(115, (i) => "index $i");
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Index Example'),
),
body: ListView.separated(
padding: const EdgeInsets.all(8),
itemCount: items.length,
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text(items[index 1]), <---here on this line I get the error.
);
},
separatorBuilder: (BuildContext context, int index) => const Divider(),
),
);
}
}
CodePudding user response:
As Abdelkrim Bournane said in his answer arrays in dart are zero-based just use Text(items[index])
, but if what your are trying to do is skipping first item in the lest and use the rest of it your list will be something like this
ListView.separated(
padding: const EdgeInsets.all(8),
itemCount: items.length -1 , // <-----
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text(items[index 1]),
);
},
separatorBuilder: (BuildContext context, int index) => const Divider(),
),
CodePudding user response:
Why would you use index 1? What's your intention? Array in dart are zero-based, so you should use the index directly without adding 1, like this :
title: Text(items[index])
CodePudding user response:
Array in dart are starting with zero index only, so if you want to display the text based on index and starts with one then try the following code
//In array declaration
final List.generate(115, (i) => "index ${i 1}");
//While displaying the text just use index not index 1
Text(items[index])