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

The refresh() method throws multiple queries when using the infinite_scroll_pagination package #221

Closed atasumt closed 1 year ago

atasumt commented 2 years ago

I am using the infinite_scroll_pagination package but i am having trouble. My problem is as follows: I have two different pages, one is a list of products, one is a list of filters, after going to the filter list and calling the controller.refresh() method, the package I use when returning is throwing multiple interrupt queries, while doing this, the number of times I enter the filter page, the more requests I get. . my code is below. Could you help?

`final PagingController<int, CompanyProductsDatum> pagingSellerPageController = PagingController(firstPageKey: 1);

  @override
  late Rx<Future<List<Object?>>> mainFuture = Future.wait([]).obs;

  @override
  void onInit() {
    // TODO: implement onInit
    updateMainFuture();
    super.onInit();
  }

@override void dispose() { // TODO: implement dispose super.dispose(); pagingSellerPageController.dispose(); }


  @override
  void updateMainFuture() {
    // TODO: implement updateMainFuture
    pagingSellerPageController.addPageRequestListener((pageKey) {
        sellerPageProduct(pageKey);
    });

    Future<void> sellerPageProduct(int page) async {
    filterCategoryIds.value.add('${tagButtonController.value}');
    try {
      final newItems = await sellerPageProductService(
        companyId: Get.parameters["id"].toString(),
        page: page.toString(),
        categoryIds: filterCategoriesKey.value.currentState?.fields['categories']?.value ?? filterCategoryIds.value,
        brandsIds: filterBrandsKey.value.currentState?.fields['brands']?.value,
        sortColumn: sortColumn(),
        minPriceProducts: minSale(),
        keyword: sellerPageSearchFormkey.value.currentState?.fields['sellerpagesearch']?.value ?? '',
      ).then((res) => sellerPageProductsData.value = res);
      final isLastPage = newItems.paginate!.page == newItems.paginate!.pages;
      if (isLastPage) {
        pagingSellerPageController.appendLastPage(newItems.companyProductsData ?? []);
      } else {
        final nextPageKey = page + 1;
        pagingSellerPageController.appendPage(newItems.companyProductsData ?? [], nextPageKey);
      }
      update(['sellerpageproducts']);
    } catch (error) {
      pagingSellerPageController.error = 'Errorrr';
    }
  }

   GlobalElevatedButton(
                height: 36,
                width: context.width * 0.3,
                onPressed: () async {
                  controller.pagingSellerPageController.refresh();

                  await Get.offNamed('/sellerPage/?id=${Get.parameters["id"].toString()}');
                },
                text: 'Filtrele',
                textStyle: Theme.of(context).textTheme.subtitle2!.copyWith(
                      color: kBgColor,
                      fontSize: 12,
                    ),
                style: ButtonStyle(
                  shape: MaterialStateProperty.all<OutlinedBorder>(RoundedRectangleBorder(borderRadius: BorderRadius.circular(30))),
                  backgroundColor: MaterialStateProperty.all<Color>(kPrimaryColor),
                ),
              ),`
samtuga1 commented 1 year ago

I'm also facing a similar issue. Did you find any solutions yet?

atasumt commented 1 year ago

I think the problem is reloading the page. I checked and edited my root structure. I avoided page reload using flutter_hooks