Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pageController addPageRequestListener is called 4 times #143

Closed
HaardikBhagtani opened this issue Oct 26, 2021 · 8 comments
Closed

pageController addPageRequestListener is called 4 times #143

HaardikBhagtani opened this issue Oct 26, 2021 · 8 comments

Comments

@HaardikBhagtani
Copy link

As soon app opens addPageRequestListener is called 4x times and 4x times data is shown without reaching at the end of the first page or either scrolling, as I scroll forward, more data loads automatically without reaching at the end of the page.

import 'dart:convert';
import 'package:balaloka/constants/api_links.dart';
import 'package:balaloka/modals/video_details.dart';
import 'package:balaloka/widgets/video_card.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:http/http.dart' as http;
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';

class CommonVideoListing extends StatefulWidget {
  final int favouriteVideos;
  final int userId;
  CommonVideoListing({required this.userId, required this.favouriteVideos});
  @override
  _CommonVideoListingState createState() => _CommonVideoListingState();
}

class _CommonVideoListingState extends State<CommonVideoListing> {
  late Video video;
  final PagingController<int, Video> _pagingController = PagingController(
    firstPageKey: 0,
  );
  static const _pageSize = 10;
  int pageNumber = 1;

  @override
  void dispose() {
   _pagingController.dispose();
    super.dispose();
  }

  @override
  void initState() {
    super.initState();
    _pagingController.addPageRequestListener((pageKey) {
     
      listingDataAPI(pageKey);
    });
   
  }

  Future<void> listingDataAPI(int pageKey) async {
    if (widget.userId == -100) {
      final SharedPreferences sharedPreferences =
          await SharedPreferences.getInstance();
      var parentId = sharedPreferences.getString('parentId');

      try {
        http.Response response = await http.get(Uri.parse(API.videoListingsAPI +
            'UserId=$parentId&queryText=&onlyfavouritevideos=${widget.favouriteVideos}&pagenumber=$pageNumber&pagesize=10'));
        var parsed = jsonDecode(response.body);
        video = Video.fromJson(parsed);

        final newItems = [video];
        final isLastPage = newItems[0].videoDetails.length < _pageSize;
        if (isLastPage) {
          _pagingController.appendLastPage(newItems);
        } else {
          final nextPageKey = pageKey + newItems[0].videoDetails.length;
          _pagingController.appendPage(newItems, nextPageKey);
        }
        setState(() {
          pageNumber += 1;
        });
      } catch (error) {
        _pagingController.error = error;
      }
    }

  }


  @override
  Widget build(BuildContext context) {
    final MediaQueryData queryData = MediaQuery.of(context);
    final width = queryData.size.width;
    return Padding(
        padding: EdgeInsets.only(
          left: 0.05 * width,
          right: 0.05 * width,
        ),
        child: CustomScrollView(
          slivers: [
            PagedSliverList<int, Video>(
              pagingController: _pagingController,
              builderDelegate: PagedChildBuilderDelegate<Video>(
                itemBuilder: (context, item, i) {
                  return ListView.builder(
                    controller: ScrollController(),
                    shrinkWrap: true,
                    itemCount: item.videoDetails.length,
                    itemBuilder: (BuildContext context, int index) {
                      return VideoCard(
                        video: item,
                        context: context,
                        index: index,
                        userId: widget.userId,
                        videoId: -100,
                      );
                    },
                  );
                },
                noItemsFoundIndicatorBuilder: (context) {
                  return Text('No More Videos Found');
                },
                noMoreItemsIndicatorBuilder: (context) {
                  return Text('No More Videos Found');
                },
                newPageProgressIndicatorBuilder: (context) {
                  return Center(
                    child: CircularProgressIndicator(),
                  );
                },
                firstPageProgressIndicatorBuilder: (context) {
                  return Center(
                    child: CircularProgressIndicator(),
                  );
                },
              ),
            ),
          ],
        )
  }
}
@EdsonBueno
Copy link
Owner

Try increasing the height of your VideoCard and tell me if the same happens. Thanks.

@HaardikBhagtani
Copy link
Author

Actually, 1 item id having 10 VideoCard 's, and initial value of invisibleItemsThreshold was 3, so 4x data was shown, now I changed invisibleItemsThreshold to 1 so only 2x data is shown i.e. 20 VideoCard 's.
Video Card height: 0.3 * height, already.
I want to show like, at the 10th video card, next 10 video cards should be made i.e. api should be called.
Thanks.

@Linkadi98
Copy link

Linkadi98 commented Nov 7, 2021

I have same problem with @HaardikBhagtani, if I ignore the invisibleItemsThreshold, 4 times api calling, if invisibleItemsThreshold is set to 1, then 2 times api calling. Once again, I set invisibleItemsThreshold to 0, there is just one api calling, and I can't load more data when swipe up. I can see that in Raywhenderlich's sample project, they performed everything correctly, except one thing they used the older version of your lib (2.2.1)

@Linkadi98
Copy link

Linkadi98 commented Nov 7, 2021

So what are the differences between the 2.2.1 version and the latest one? @EdsonBueno

@Linkadi98
Copy link

Oh Raywhenderlich's sample project had the same problem with us now, I changed their page size to 1 and the pagingController started to call api so many times. Something is wrong now. @EdsonBueno

@HaardikBhagtani
Copy link
Author

I have same problem with @HaardikBhagtani, if I ignore the invisibleItemsThreshold, 4 times api calling, if invisibleItemsThreshold is set to 1, then 2 times api calling. Once again, I set invisibleItemsThreshold to 0, there is just one api calling, and I can't load more data when swipe up. I can see that in Raywhenderlich's sample project, they performed everything correctly, except one thing they used the older version of your lib (2.2.1)

Yes, you are right. same thing is happening.

@EdsonBueno
Copy link
Owner

I'm not sure I'm following @Linkadi98 @HaardikBhagtani .
If you change the pageSize to 1, that means each page will only have one item, so the controller has to call the API as many times as it needs to fill up the screen.

Can either one of you provide me a minimal sample project reproducing the issue?

@EdsonBueno
Copy link
Owner

Closing this due to inactivity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants