handlePageChange method Null safety

  1. @override
Future<bool> handlePageChange(
  1. int oldPageIndex,
  2. int newPageIndex
)
override

Called when the page is navigated.

Return true, if the navigation should be performed. Otherwise, return false to disable the navigation for specific page index.

Implementation

@override
Future<bool> handlePageChange(int oldPageIndex, int newPageIndex) {
  if (effectiveRows.isEmpty || _pageCount == 0) {
    _paginatedRows = <DataGridRow>[];
    return Future<bool>.value(true);
  }

  final DataGridConfiguration dataGridConfiguration =
      _dataGridStateDetails!();

  final int rowsPerPage = dataGridConfiguration.rowsPerPage ??
      (effectiveRows.length / _pageCount).ceil();
  final int _startIndex = newPageIndex * rowsPerPage;
  int _endIndex = _startIndex + rowsPerPage;

  /// Need to calculate endIndex for the last page, when the number of rows is
  /// lesser than rowsPerPage.
  if (_endIndex > effectiveRows.length) {
    _endIndex = effectiveRows.length;
  }

  /// Get particular range from the sorted collection.
  if (_startIndex < effectiveRows.length &&
      _endIndex <= effectiveRows.length) {
    _paginatedRows = effectiveRows.getRange(_startIndex, _endIndex).toList();
  } else {
    _paginatedRows = <DataGridRow>[];
  }

  /// Updates the collection in data pager.
  notifyListeners();

  return Future<bool>.value(true);
}