Home > Software engineering >  Restrict list for table - Flutter
Restrict list for table - Flutter

Time:02-11

I want to limit the number of rows in the table, because I get about 40 rows from the API, and I need to make 5 rows. Tell me, what method should be applied in List.generate to show only the first 5 elements from the list? Thanks in advance.

DataTable

child: FutureBuilder<List<BlockModel>>(
              future: futureBlock,
              builder: (context, snapshot) {
                if (snapshot.hasData) {
                  return DataTable(
                      columns: const [
                        DataColumn(label: Text('Height')),
                        DataColumn(label: Text('Hash')),
                        DataColumn(label: Text('Time')),
                      ],
                      rows: List.generate(snapshot.data!.length, (index) {
                        return DataRow(cells: [
                          DataCell(Text('${snapshot.data![index].height}')),
                          DataCell(Text(snapshot.data![index].hash)),
                          DataCell(Text('${snapshot.data![index].time}')),
                        ]);
                      })
                      );
                } else {

CodePudding user response:

The first parameter of List.generate is the length. You currently wrote snapshot.data!.length, if you want the first 5, just put 5 there.

CodePudding user response:

You can restrict the length of the List while generating it.

rows: List.generate(5, (index) {

// ---Your Code---

}

Here you can see instead of snapshot.data!.length you can pass the predefined length. Please apply the logic yourself if the snapshot.data.length is lesser than five

  • Related