Flutter PaginatedDataTable - DataTableSource not sorting properly when the data changes

321 views Asked by At

I'm trying to create a paginatedDataTable that can be filtered, however if the data is filtered, the sorting doesn't work.

after adding some print checks, it shows that the data is already filtered & sorted properly, however the notifyListeners() inside the DataTableSource doesn't seem to be updating the view. However, it is working properly when the data isn't filtered.

this is the sort function inside the DataTableSource:

  void _sort<T>(Comparable<T> getField(Item item), bool ascending) {
    print("sort data ${ascending ? "ascending" : "descending"}");
    print("item list size: ${_itemList.length}");
    _itemList.sort((Item a, Item b) {
      final aValue = getField(a);
      final bValue = getField(b);
      return ascending ? Comparable.compare(aValue, bValue) : Comparable.compare(bValue, aValue);
    });
    notifyListeners();
  }

this is how the data is filtered:

_itemData = _provider.item.where((element) => element.code.toLowerCase().contains(filterText.toLowerCase())).toList();

_provider is the data Provider that keeps track of the data changes using ChangeNotifier which is then passed to a DataSource:

_itemDataSource = ItemDataSource(item: _itemData, context: context);
...
source: _itemDataSource,

output:

flutter: sort data descending
flutter: item list size: 1186
flutter: sort data ascending
flutter: item list size: 1186
flutter: sort data descending
flutter: item list size: 24
flutter: sort data ascending
flutter: item list size: 24

Any suggestion or help is greatly appreciated

0

There are 0 answers