Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell itemBuilder there is no more items in Flutter?

Tags:

flutter

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?

like image 641
camino Avatar asked Oct 17 '25 20:10

camino


2 Answers

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'));
    },
  );
}
like image 199
Rémi Rousselet Avatar answered Oct 20 '25 11:10

Rémi Rousselet


You just need return null in your itemBuilder function when there is no more item to build.

like image 26
Bono Lv Avatar answered Oct 20 '25 11:10

Bono Lv



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!