EdsonBueno / infinite_scroll_pagination

Flutter package to help you lazily load and display pages of items as the user scrolls down your screen.
https://pub.dev/packages/infinite_scroll_pagination
MIT License
623 stars 210 forks source link

Refreshing clears previous listview #188

Closed Veeksi closed 1 year ago

Veeksi commented 2 years ago

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

clragon commented 2 years 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.

Veeksi commented 2 years 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.

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.

lauglam commented 2 years ago

@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 commented 2 years ago

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

sandifb commented 1 year ago

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

EdsonBueno commented 1 year ago

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