When going through the sample in flutter tutorial
class RandomWordsState extends State<RandomWords> {
...
Widget _buildSuggestions() {
return new ListView.builder(
padding: const EdgeInsets.all(16.0),
itemBuilder: (context, i) {
if (i.isOdd) return new Divider();
final index = i ~/ 2;
if (index >= _suggestions.length) {
_suggestions.addAll(generateWordPairs().take(10));
}
return _buildRow(_suggestions[index]);
}
);
}
}
I am wondering if there is a way to tell the itemBuilder
that there are no more items. For example, I only want to show 30 items in the listview, how can I do that?
There's a property on this constructor called itemCount
.
You can set it to whatever value you like to limit ListView.builder
/ListView.separated
size.
Here's an example a limits a ListView
to 30 items
Widget build(BuildContext context) {
return ListView.builder(
itemCount: 30,
itemBuilder: (context, index) {
return ListTile(title: Text('index: $index'));
},
);
}
You just need return null
in your itemBuilder function when there is no more item to build.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With