Closed Veeksi closed 1 year ago
this is a similar issue as #115.
you can solve your problem by refreshing, but only replacing the data on success. as of right now, this functionality isnt supported, but you can implement it yourself.
one way of doing that would be overriding the refresh
method of the PagingController
:
@override
Future<void> refresh({bool background = false}) async {
if (background) {
try {
final items = fetchItems(firstPageKey);
value = PagingState(
nextPageKey: nextPageKey,
itemList: items,
error: null,
);
} catch (e) {
error = e;
}
} else {
super.refresh();
}
}
or alternatively, you could add an extension method,
like backgroundRefresh
, to the PagingController
without the if statement.
this is a similar issue as #115.
you can solve your problem by refreshing, but only replacing the data on success. as of right now, this functionality isnt supported, but you can implement it yourself.
one way of doing that would be overriding the
refresh
method of thePagingController
:@override Future<void> refresh({bool background = false}) async { if (background) { try { final items = fetchItems(firstPageKey); value = PagingState( nextPageKey: nextPageKey, itemList: items, error: null, ); } catch (e) { error = e; } } else { super.refresh(); } }
or alternatively, you could add an extension method, like
backgroundRefresh
, to thePagingController
without the if statement.
I don't see how this can be used when using state management solution like BLoC where you listen state changes and based on that get results. This would work, if you make data requests in UI level.
@Veeksi Extends and override these methods to get what you want
class PagingControllerExtent<PageKeyType, ItemType>
extends PagingController<PageKeyType, ItemType> {
PagingControllerExtent({
required super.firstPageKey,
super.invisibleItemsThreshold,
});
/// Whether it is in the state of background refresh
bool background = false;
@override
void refresh({bool background = false}) {
if (background) {
this.background = true;
// Since the state of PagingState has too much influence,
// skip it here and directly notifyPageRequestListeners.
notifyPageRequestListeners(firstPageKey);
} else {
super.refresh();
}
}
@override
void appendPage(List<ItemType> newItems, PageKeyType? nextPageKey) {
// Add judgment here
final previousItems = background ? <ItemType>[] : value.itemList ?? <ItemType>[];
final itemList = previousItems + newItems;
value = PagingState<PageKeyType, ItemType>(
itemList: itemList,
error: null,
nextPageKey: nextPageKey,
);
// reduction
background = false;
}
}
This is a very unofficial practice, so if there is a better way please let me know.
any update ? i have clickable chip, for get new data form API and also filter the list.
Closing as this has nothing to do with the package itself but w/ Flutter usage in general.
How should I check before I refresh my listview that I have internet connection because otherwise I lose all that data in previous listview?