I have a list of user-favorited words I would like to be shown on the screen, and I used a ListView.builder to do this. It looks something like this:
ListView.builder(
itemBuilder(context, i) {
return ListTile(
title: Text(words[i])
)})
The issue is, eventually I run out of words, and then I get an error. I would really like it if the ListView would simply be able to stop trying to add new widgets and not be able to scroll past what has been already generated. Is this possible, or should I look to a different type of display widget?
Thanks!
CodePudding user response:
Limit the number of ListView
with itemCount
.
List<String> words = [
'one',
'two',
'three',
'four',
'five',
'six',
'seven',
'eight',
'nine',
'ten',
'1',
'2',
'3',
'4'
];
return Scaffold(
body: SafeArea(
child: ListView.builder(
itemCount: words.length,
itemBuilder: (context, index) => ListTile(title: Text(words[index])),
),
),
);
}
CodePudding user response:
ListView.builder
has a property itemCount
where you can give the length of items the List
will contain.
In your example:
ListView.builder(
// Here specify the number of items
itemCount: words.length,
itemBuilder(context, i) {
return ListTile(
title: Text(words[i])
)})