diff --git a/CHANGELOG.md b/CHANGELOG.md index e369807..69700db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ## Upcoming +- [[#7](https://github.com/xsahil03x/super_paging/issues/7)] Fix generating load trigger notification when `itemCount` is less than `prefetchIndex`. - Added support for the `Pager.refresh` method to accept an optional `refreshKey` parameter. ## 0.1.0 diff --git a/lib/src/widget/bidirectional_paging_list_view.dart b/lib/src/widget/bidirectional_paging_list_view.dart index ac5d645..5e30e6d 100644 --- a/lib/src/widget/bidirectional_paging_list_view.dart +++ b/lib/src/widget/bidirectional_paging_list_view.dart @@ -463,24 +463,31 @@ class _BidirectionalPagingListViewState }) { final items = pages.items; final itemCount = items.length; - final fetchIndex = pager.config.prefetchIndex; + final prefetchIndex = pager.config.prefetchIndex; // Helper function to generate prepend and append load trigger notifications void generatePrependAppendLoadTriggerNotification(int index) { // If there is no prefetch index, we don't need to generate any // notifications. - if (fetchIndex == null) return; - - // Check if the index corresponds to near the top or bottom based on the - // 'reverse' flag. - final nearTop = - reverse ? index == itemCount - fetchIndex : index == fetchIndex; - final nearBottom = - reverse ? index == fetchIndex : index == itemCount - fetchIndex; + if (prefetchIndex == null) return; + + // Generate notifications at the beginning and end of the list if the + // [itemCount] is less than [prefetchIndex]. + if (prefetchIndex > itemCount) { + if (index == 0) onBuildingPrependLoadTriggerItem?.call(); + if (index == itemCount - 1) onBuildingAppendLoadTriggerItem?.call(); + return; + } + + // Check if the index corresponds to near the top or bottom of the list + // based on the [reverse] flag. + final (nearTop, nearBottom) = switch (reverse) { + true => (index == itemCount - prefetchIndex, index == prefetchIndex), + false => (index == prefetchIndex, index == itemCount - prefetchIndex), + }; - // Generate prepend notification. + // Generate notifications. if (nearTop) onBuildingPrependLoadTriggerItem?.call(); - // Generate append notification. if (nearBottom) onBuildingAppendLoadTriggerItem?.call(); } diff --git a/lib/src/widget/paging_sliver_list.dart b/lib/src/widget/paging_sliver_list.dart index 95830a2..4ce01ae 100644 --- a/lib/src/widget/paging_sliver_list.dart +++ b/lib/src/widget/paging_sliver_list.dart @@ -265,20 +265,29 @@ class _PagingSliverListState final itemCount = items.length; final prefetchIndex = pager.config.prefetchIndex; + // Helper function to generate prepend and append load trigger notifications void generatePrependAppendLoadTriggerNotification(int index) { // If there is no prefetch index, we don't have to generate any // notification. if (prefetchIndex == null) return; - // Generate prepend notification. - if (index == prefetchIndex) { - onBuildingPrependLoadTriggerItem?.call(); + // Generate notifications at the beginning and end of the list if the + // [itemCount] is less than [prefetchIndex]. + if (prefetchIndex > itemCount) { + if (index == 0) onBuildingPrependLoadTriggerItem?.call(); + if (index == itemCount - 1) onBuildingAppendLoadTriggerItem?.call(); + return; } - // Generate append notification. - if (index == itemCount - prefetchIndex) { - onBuildingAppendLoadTriggerItem?.call(); - } + // Check if the index corresponds to near the top or bottom of the list. + final (nearTop, nearBottom) = ( + index == prefetchIndex, + index == itemCount - prefetchIndex, + ); + + // Generate notifications. + if (nearTop) onBuildingPrependLoadTriggerItem?.call(); + if (nearBottom) onBuildingAppendLoadTriggerItem?.call(); } final itemBuilder = widget.itemBuilder;