FadyFayezYounan / easy_infinite_pagination

A simple and customizable infinite pagination package for Flutter applications. The package provides a simple and customizable way to implement infinite pagination in your Flutter applications.
MIT License
3 stars 2 forks source link

unable to do range pagination with Supabase & the provided bloC exmaple #1

Open tiltmaster opened 6 months ago

tiltmaster commented 6 months ago

So i have been trying your package and i have been facing issues, using your bloC example, i have been doing as such : however, im unable to get unique items and or new items to begin with, knowing that calling _fetchPosts is


Future<void> fetchPosts({bool refresh = false}) async {
    if (refresh) {
      emit(state.invalidateSelf());
    } else {
      emit(state.copyWith(isLoading: true));
    }

Future<void> fetchPosts({bool refresh = false}) async {
    if (refresh) {
      emit(state.invalidateSelf());
    } else {
      emit(state.copyWith(isLoading: true));
    } 
    try {
      // Fetch posts from the API.
      final posts = await _fetchPosts(page: state.page);
      // Update the state with the fetched posts, the next page number,
      // and the hasReachedMax flag.
      emit(state.copyWith(
        isLoading: false,
        posts: List.of(state.posts)..addAll(posts),
        hasReachedMax: posts.length > 25,
        page: state.page + 5,
        hasError: false,
      ));
    } catch (e) {
      // If an error occurs, update the state with the hasError flag set to true.
      emit(state.copyWith(isLoading: false, hasError: true));
    }
  }```

not sure if this is an issue or im doing something wrong.
FadyFayezYounan commented 6 months ago

@tiltmaster, change the condition 'hasReachedMax: posts.length > 25' to 'hasReachedMax: posts.length < 25' and let me know if it works correctly.

tiltmaster commented 6 months ago

@tiltmaster, change the condition 'hasReachedMax: posts.length > 25' to 'hasReachedMax: posts.length < 25' and let me know if it works correctly.

I actually have it as such, see the below code for example:


Future<void> fetchPosts({bool refresh = false}) async {
    if (refresh) {
      emit(state.invalidateSelf());
    } else {
      emit(state.copyWith(isLoading: true));
    }

    try {
      const pageSize = 5;
      late int start;
      final end = state.page * pageSize;
      if (state.page > 1) {
        start = end - pageSize + 1;
      } else {
        start = end - pageSize;
      }

      final posts = await _fetchPosts(start: start, end: end);
      final maxLength = await supa.Supabase.instance.client.from('posts').count().eq('is_deleted', false);
      emit(state.copyWith(
        isLoading: false,
        posts: List.of(state.posts)..addAll(posts),
        hasReachedMax: posts.length < maxLength,
        page: state.page + 1,
        hasError: false,
      ));
    } catch (e) {
      // If an error occurs, update the state with the hasError flag set to true.
      emit(state.copyWith(isLoading: false, hasError: true));
    }
  }

  Future<List<Posts>> _fetchPosts({
    required int start,
    required int end,
    int limit = 5,
  }) async {
    try {
      print('Start:$start End:$end');
      final response = await supa.Supabase.instance.client.from('posts').select().eq('is_deleted', false).order('timestamp').range(start, end);
      return response.map((dynamic json) => Posts.fromMap(json['id'], json)).toList();
    } catch (e) {
      throw Exception('Something went wrong while fetching posts');
    }
  }```
and its returning duplicates, the reason i did the start and end is to do pagination,
tiltmaster commented 6 months ago

@tiltmaster, change the condition 'hasReachedMax: posts.length > 25' to 'hasReachedMax: posts.length < 25' and let me know if it works correctly.

When following ur example, and your suggestion, the hasReachedMax returns true. even tho that for example in the above shared code maxLength for example returns 50 posts, its returning true but its not because it has yet to be more than 50 posts

EDIT: I have solved this with state.posts.length >= maxLength,