Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When fetched data length is less than page size, widget loads duplicated for same offset #39

Open
onursahindur opened this issue Apr 30, 2021 · 1 comment

Comments

@onursahindur
Copy link

First of all, appreciate for a great work. However I encountered a problem.
When the fetched data length from backend is less than the page size, for example data length for page 1 is 5 and page size is 25,
Widget loads the same page again.

Here is the code block I used for achieving the pagination.

body: SafeArea(
        child: PaginationView<Purchase>(
          paginationViewType: PaginationViewType.listView,
          itemBuilder: (BuildContext context, Purchase history, int index) => listItem(context, history, index),
          pageFetch: fetchSearchHistory,
          shrinkWrap: true,
          pullToRefresh: true,
          onError: (dynamic error) => Container(
              height: 70,
              alignment: Alignment.center,
              child: Text('Some error occured.', style: TextStyle(
                color: Colors.black,
                fontSize: 20,
                fontWeight: FontWeight.w400,
              )),
          ),
          onEmpty: Container(
              height: 70,
              alignment: Alignment.center,
              child: Text('No past searches', style: TextStyle(
                color: Colors.black,
                fontSize: 20,
                fontWeight: FontWeight.w400,
              )),
          ),
          bottomLoader: Center(
            child: CircularProgressIndicator(),
          ),
          initialLoader: Container(
              height: 70,
              alignment: Alignment.center,
              child: CircularProgressIndicator(),
          ),
        ),
      )

And the page pageFetch method is:

Future<List<Purchase>> fetchSearchHistory(offset) async {
    var page = (offset / 25).round() + 1;
    print(page);
    try {
      return await ApiServices.purchaseHistory(page);
    } catch (error) {
      print(error);
      return [];
    }
  }

@vedartm
Copy link
Owner

vedartm commented May 8, 2021

Hey @onursahindur! I suspect it is because of the page calculation you are doing. Try this

var page = (offset / 25).ceil() + 1;

Also, make sure your API returns an empty list when you fetch the next page which has no data (in this above example, query with page = 2 should return empty list). Because returning an empty list to pageFetch stops the pagination.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants