diff --git a/examples/flutter_infinite_list/lib/posts/bloc/post_bloc.dart b/examples/flutter_infinite_list/lib/posts/bloc/post_bloc.dart index 89f437fa0ef..36dcfc0c6ab 100644 --- a/examples/flutter_infinite_list/lib/posts/bloc/post_bloc.dart +++ b/examples/flutter_infinite_list/lib/posts/bloc/post_bloc.dart @@ -23,45 +23,38 @@ EventTransformer throttleDroppable(Duration duration) { class PostBloc extends Bloc { PostBloc({required this.httpClient}) : super(const PostState()) { on( - _onPostFetched, + _onFetched, transformer: throttleDroppable(throttleDuration), ); } final http.Client httpClient; - Future _onPostFetched( + Future _onFetched( PostFetched event, Emitter emit, ) async { if (state.hasReachedMax) return; + try { - if (state.status == PostStatus.initial) { - final posts = await _fetchPosts(); - return emit( - state.copyWith( - status: PostStatus.success, - posts: posts, - hasReachedMax: false, - ), - ); + final posts = await _fetchPosts(startIndex: state.posts.length); + + if (posts.isEmpty) { + return emit(state.copyWith(hasReachedMax: true)); } - final posts = await _fetchPosts(state.posts.length); - posts.isEmpty - ? emit(state.copyWith(hasReachedMax: true)) - : emit( - state.copyWith( - status: PostStatus.success, - posts: List.of(state.posts)..addAll(posts), - hasReachedMax: false, - ), - ); + + emit( + state.copyWith( + status: PostStatus.success, + posts: [...state.posts, ...posts], + ), + ); } catch (_) { emit(state.copyWith(status: PostStatus.failure)); } } - Future> _fetchPosts([int startIndex = 0]) async { + Future> _fetchPosts({required int startIndex}) async { final response = await httpClient.get( Uri.https( 'jsonplaceholder.typicode.com',