infinite_scroll_pagination icon indicating copy to clipboard operation
infinite_scroll_pagination copied to clipboard

Refreshing clears previous listview

Open Veeksi opened this issue 3 years ago • 4 comments

How should I check before I refresh my listview that I have internet connection because otherwise I lose all that data in previous listview?

Veeksi avatar Apr 14 '22 20:04 Veeksi

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.

clragon avatar Apr 23 '22 11:04 clragon

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.

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 avatar May 20 '22 13:05 Veeksi

@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;
  }
}

lauglam avatar Jun 15 '22 11:06 lauglam

This is a very unofficial practice, so if there is a better way please let me know.

lauglam avatar Jun 15 '22 11:06 lauglam

any update ? i have clickable chip, for get new data form API and also filter the list.

sandifb avatar Jan 31 '23 05:01 sandifb

Closing as this has nothing to do with the package itself but w/ Flutter usage in general.

EdsonBueno avatar Mar 02 '23 22:03 EdsonBueno