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

Changing underlying list can cause exception to be thrown #296

Closed lukehutch closed 8 months ago

lukehutch commented 8 months ago

paged_layout_builder.dart includes this code:

              case PagingStatus.ongoing:
                child = widget.loadingListingBuilder(
                  context,
                  // We must create this closure to close over the [itemList]
                  // value. That way, we are safe if [itemList] value changes
                  // while Flutter rebuilds the widget (due to animations, for
                  // example.)
                  (context, index) => _buildListItemWidget(
                    context,
                    index,
                    itemList!,
                  ),
                  _itemCount,
                  _newPageProgressIndicatorBuilder,
                );
                break;

(and then similar cases for the other PagingStatus values).

The problem with this is that if you do something like the following

    pagingController.value = PagingState(
      error: null,
      itemList: newList,
      nextPageKey: newList == null
          ? null
          : newList!.length,
    );

... and if newList is shorter than the old itemList, then you get an exception when the builder tries to access an index that is out of range. For example, I get an index out of range exception at this line in paged_layout_builder.dart:

    final item = itemList[index];

The reason is that the old the itemList was captured in the closure, but the new item count is obtained from the new list.

So actually this builder pattern breaks for the opposite of the intended reason.

lukehutch commented 8 months ago

I'll close this, since I assume the library simply wasn't written to allow the underlying list to be completely changed like this.