Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter, Dart: How to show only the first 10 items in list view builder?

Tags:

flutter

dart

I want to Show only the first ten items in a listview builder, How do I do that.

ListView.builder(
    itemCount: data == null ? 0 : data.length,
    itemBuilder: (BuildContext context, int index) {
      if (data[index]['population'] >= 100000) {
        return new Card(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              new Text(data[index]["name"]),
              new Text(data[index]["population"].toString()),
              new Text(data[index]["latlng"].toString()),
            ],
          ),
        );
      } else {
        return Container();
      }
like image 527
Sathak Ahmed Faizal Avatar asked Nov 01 '25 12:11

Sathak Ahmed Faizal


1 Answers

This is how you would generate the first ten items in a listview builder.

ListView.builder(
itemCount: data == null ? 0 : (data.length > 10 ? 10 : data.length),
itemBuilder: (BuildContext context, int index) {
  if (data[index]['population'] >= 100000) {
    return new Card(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          new Text(data[index]["name"]),
          new Text(data[index]["population"].toString()),
          new Text(data[index]["latlng"].toString()),
        ],
      ),
    );
  } else {
    return Container();
  }
like image 127
Aloysius Samuel Avatar answered Nov 03 '25 14:11

Aloysius Samuel



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!