Skip to content

Commit

Permalink
simplify PostFetched event handler
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasMirbt committed Aug 23, 2024
1 parent 3ed67d3 commit 13a0fee
Showing 1 changed file with 15 additions and 22 deletions.
37 changes: 15 additions & 22 deletions examples/flutter_infinite_list/lib/posts/bloc/post_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,45 +23,38 @@ EventTransformer<E> throttleDroppable<E>(Duration duration) {
class PostBloc extends Bloc<PostEvent, PostState> {
PostBloc({required this.httpClient}) : super(const PostState()) {
on<PostFetched>(
_onPostFetched,
_onFetched,
transformer: throttleDroppable(throttleDuration),
);
}

final http.Client httpClient;

Future<void> _onPostFetched(
Future<void> _onFetched(
PostFetched event,
Emitter<PostState> 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<List<Post>> _fetchPosts([int startIndex = 0]) async {
Future<List<Post>> _fetchPosts({required int startIndex}) async {
final response = await httpClient.get(
Uri.https(
'jsonplaceholder.typicode.com',
Expand Down

0 comments on commit 13a0fee

Please sign in to comment.