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

Conflicting with Bloc | Bad state: Cannot add new events after calling close #294

Closed mister-rao closed 8 months ago

mister-rao commented 8 months ago

This is a weird behaviour that I have noticed while using the package. When using with a page that has other widgets adding events to bloc, I get Bad state: Cannot add new events after calling close error after scrolling down to page 2.

Initially I was adding fetch event to bloc using:

  pagingController.addPageRequestListener((pageKey) {
      bloc.add(FetchPage(pageKey))
    });

And updated the pagecontroller state in BlocListener

This gave me the "Bad State" error after scrolling down to till page 2, after failing to figure out what went wrong, I removed all bloc dependency for calling the Api, and called it directly from the Api repo. This fixed the issue, and I can scroll till the end. Works like a charm.

But when I tried to refresh the page using refresh indicator, that adds events to the different blocs to refresh other widgets in the page, I get the Bad state error again.

Again, this only happens after I scroll down till page 2 items. If refresh the page without scrolling down, the blocs work fine.

Below are how my widgets are laid out:

ProfilePage (Stateful Widget)
- BlocConsumer
-- Refresh Indicator
--- CustomScrollView
---- SliverAppBar
---- ProfileCard (Wrapped with SliverToBoxAdapter)
---- ProfileInfo (Wrapped with SliverToBoxAdapter)
---- ProfileTimeline (StatefulWidget containing PagedSliverGrid)
mister-rao commented 8 months ago

It was because of HookWidget. 🤦Closing this.