Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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.