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
632 stars 214 forks source link

Add a caching example #181

Closed FarhanSajid1 closed 1 year ago

FarhanSajid1 commented 2 years ago

Hey there, I'm new to flutter and this package seems highly extensible and can fit a large number of use cases. I was wondering if an example could be added for caching widgets with this package, it seems like when I scroll up/down widgets are reloaded which results in additional api calls to be sent which is not expected. I've gone through the example article here: https://www.raywenderlich.com/14214369-infinite-scrolling-pagination-in-flutter and the cookbook, but have not seen how to do this with this package.

I've also tried creating a map and check if the index exists before the builder runs, but that also does not seem to work

                    child: PagedListView(
                        pagingController: homeController.pagingController,
                        builderDelegate: PagedChildBuilderDelegate<Timeline>(
                            animateTransitions: true,
                            itemBuilder: (context, item, index) {
                              if (homeController.cache.containsKey(index)) {
                                print('found in cache!');
                                return homeController.cache[index]!;
                              }
                              print('not found in cache, calling');
                              var match = MatchWidget(
                                  match: item.MatchWidget!);
                              homeController.cache.addIf(true, index, match);
                              return match;
                            })),

Could someone point me to a snippet of code, I can add it to the repo after. Thank you in advance.

clragon commented 2 years ago

you should not be caching widgets, your data should be held in the controller and widgets should be built from it. the widgets will be disposed and built as they come and go into the viewport. this is expected as having infinite widgets built will quickly degrade the performance of your app as the user scrolls down. it is fundamentally incomaptible with infinite pagination. the widgets inside of the builder delegate should optimally also not make API requests. if they still have to, you can cache those requests instead, for example in a http client like dio with an interceptor.