Closed lucasshiva closed 10 months ago
Figured out the error. It was totally my fault. I needed to put the try/catch inside the addPageRequestListener
instead of inside the cubit itself. Like so:
pagingController.addPageRequestListener((pageKey) async {
log("Fetching page: $pageKey");
try {
final itemState = await itemCubit.fetchItems(pageKey);
final isLastPage = itemState.currentPage == itemState.lastPage;
if (isLastPage) {
pagingController.appendLastPage(itemState.items);
} else {
pagingController.appendPage(itemState.items, pageKey + 1);
}
} catch (e) {
pagingController.error = e;
}
});
And also remove it from the cubit, of course:
Future<ItemState> fetchItems(int page) async {
// Simulate loading.
await Future.delayed(const Duration(milliseconds: 700));
final newItems = getItems(page);
return ItemState(
items: newItems,
currentPage: page,
lastPage: lastItemsPage,
error: null,
);
}
When an error occurs during loading subsequent pages, the package doesn't wait for the user to tap to retry—it skips the current page and loads the next page automatically.
For example: If trying to fetch page 2 returns an error, the error message is shown, but since the scroll is still at the bottom, it then tries to load page 3, completely ignoring the error of page 2.
What should happen instead: Fetching page 2 returns an error. Error widget is shown to the user. No more pages are fetched until the user taps on retry, which will show the subsequent loading again while trying to fetch the same page that returned an error, in this case, page 2.
This is my widget:
And the cubit:
For the record, the page using
ItemGrid
is a simpleStatelessWidget
with aScaffold
.This was tested on Flutter 3.13.0 stable with Dart 3.1.0. Package version is the latest 4.0.0.