Ask Your Question
4

How can Flutter paginated datatable be modified to eliminate blank rows?

asked 2023-05-19 23:41:10 +0000

lalupa gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2023-05-19 23:58:02 +0000

devzero gravatar image

One way to modify a Flutter paginated datatable to eliminate blank rows is to add a custom data source that only includes rows with data. You can do this by creating a new list of data that excludes any rows with empty values.

Here's an example:

List<Map<String, dynamic>> _data = [
  {'id': 1, 'name': 'John', 'age': 28},
  {'id': 2, 'name': '', 'age': 30},
  {'id': 3, 'name': 'Jane', 'age': null},
  {'id': 4, 'name': 'Alice', 'age': 25},
  {'id': 5, 'name': 'Bob', 'age': 35},
];

class CustomDataSource extends DataTableSource {
  int _selectedRowCount = 0;

  @override
  DataRow getRow(int index) {
    if (index >= _data.length) return null;
    final row = _data[index];
    return DataRow(
      cells: [
        DataCell(Text(row['id'].toString())),
        DataCell(Text(row['name'] ?? '')),
        DataCell(Text(row['age']?.toString() ?? '')),
      ],
    );
  }

  @override
  bool get isRowCountApproximate => false;

  @override
  int get rowCount => _data.length;

  @override
  int get selectedRowCount => _selectedRowCount;
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('DataTable Example')),
      body: PaginatedDataTable(
        header: Text('Custom Data Source'),
        columns: [
          DataColumn(label: Text('ID')),
          DataColumn(label: Text('Name')),
          DataColumn(label: Text('Age')),
        ],
        source: CustomDataSource(),
      ),
    );
  }
}

In this example, the _data list contains some rows with empty or null values. CustomDataSource implements DataTableSource and returns only the rows with non-empty values. getRow() checks if the index is less than the length of the new data list and returns null otherwise, so only the existing rows are included in the paginated table. All the other methods are implemented according to the DataTableSource interface.

By using a custom data source like this, you can eliminate the blank or null rows from your paginated datatable.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-19 23:41:10 +0000

Seen: 18 times

Last updated: May 19 '23