Sathak Ahmed Faizal
Sathak Ahmed Faizal

Reputation: 81

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

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();
      }

Upvotes: 4

Views: 6612

Answers (1)

Aloysius Samuel
Aloysius Samuel

Reputation: 1200

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();
  }

Upvotes: 6

Related Questions