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
605 stars 201 forks source link

refresh and clear data #298

Closed afl-lxw closed 5 months ago

afl-lxw commented 8 months ago

我是使用的 getx 管理 infinite_scroll_pagination 的状态

class MainController extends GetxController {
  MainController();
  final PagingController<int, RecommendationModel> pagingController =
      PagingController(firstPageKey: 0);
  @override
  void onReady() {
    super.onReady();
    _initData();
  }
  _initData() {
    pagingController.addPageRequestListener((pageKey) {
      fetchData(pageKey);
    });
    update(["main"]);
  }
  Future<void> fetchRefresh() async {
    pagingController.refresh();
  }
}

class RecommendationPage extends GetView<RecommendationController> {
  RecommendationPage({Key? key}) : super(key: key);
  final mainController = Get.put(MainController());

  Widget _buildView() {
    return RefreshIndicator(
        onRefresh: () async {
          await Future.delayed(const Duration(seconds: 1));
          mainController.fetchRefresh();
        },
        child: const RecommendTabPage());
  }
}

 // RecommendTabPage The return value is as follows
final mainController = Get.put(MainController());
PagedListView<int, RecommendationModel>.separated(
      padding: const EdgeInsets.only(bottom: 100),
      separatorBuilder: (context, index) => const SizedBox(height: 1),
      pagingController: mainController.pagingController,
      builderDelegate: PagedChildBuilderDelegate<RecommendationModel>(
        firstPageProgressIndicatorBuilder: (context) =>
            const ProgressIndicatorWidget(),
        newPageProgressIndicatorBuilder: (context) =>
            const ProgressIndicatorWidget(),
        itemBuilder: (context, item, index) {
          return RecommendItemDetails(index: index, itemData: item);
        },
      ),
    );

Now I want to execute the fetchRefresh function in getx and perform the refresh when pulling down the refresh My expectation is to clear the content and reload from the first page after execution But in reality, the data hasn't changed, just a refresh

afl-lxw commented 8 months ago

Which great god can answer my doubts or tell me what to do. Thank you.

caiombueno commented 7 months ago

Hi @afl-lxw.

Maybe you could update your fetchRefresh function to:

Future<void> fetchRefresh() async {
    pagingController.value = PagingState();
    pagingController.refresh();
  }

Let me know if it works.

Ensonberg commented 5 months ago

it doesn't work, I am currently facing the same issues, the content is duplicating on refresh

LuuNgocLan commented 5 months ago

Let's call refresh() to reset item to null first (that will execute firstPageProgressIndicatorBuilder to show ProgressIndicatorWidget) and then set new data:

Future<void> fetchRefresh() async {
    pagingController.refresh();
    pagingController.value = PagingState(
          nextPageKey: FIRST_PAGE_KEY,
          error: null,
          itemList: NEW_LIST_ITEM,
   );
  }