0
votes

That's my code for PostsBloc:

class PostsBloc extends Bloc<PostsEvent, PostsState> {
  final _dataService = DataService();

  // Constructor
  PostsBloc() : super(LoadingPostsState()) {
    on<LoadPostsEvent>((event, emit) async {
      emit(LoadingPostsState());

      try {
        final posts = await _dataService.getPosts();
        emit(LoadedPostsState(posts: posts));
      } catch (e) {
        emit(FailedToLoadPostsState(error: e));
      }
    });
  }
}

So, I want to use the same method with new event, just without emitting LoadingPostsState() like this:

 PostsBloc() : super(LoadingPostsState()) {
    on<LoadPostsEvent || PullToRefreshEvent>((event, emit) async {
      if(event == LoadPostsEvent){
        emit(LoadingPostsState());
      }

      try {
        final posts = await _dataService.getPosts();
        emit(LoadedPostsState(posts: posts));
      } catch (e) {
        emit(FailedToLoadPostsState(error: e));
      }
    });
  }