infinite_scroll_pagination icon indicating copy to clipboard operation
infinite_scroll_pagination copied to clipboard

Add a caching example

Open FarhanSajid1 opened this issue 2 years ago • 1 comments

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.

FarhanSajid1 avatar Feb 25 '22 02:02 FarhanSajid1

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.

clragon avatar Sep 02 '22 20:09 clragon