Recommand · October 14, 2021 0

How to resolve emit was called after an event handler completed normally bloc error?

I am using flutter bloc to make download progress percentage displayed but I keep getting this problem. I figured the problem arises in the onDone method but I couldn’t figure out how to fix it.


Exception has occurred.
_AssertionError (‘package:bloc/src/bloc.dart’: Failed assertion: line 137 pos 7: ‘!_isCompleted’:
emit was called after an event handler completed normally.
This is usually due to an unawaited future in an event handler.
Please make sure to await all asynchronous operations with event handlers
and use emit.isDone after asynchronous operations before calling emit() to
ensure the event handler has not completed.


 on<Event>((event, emit) {
    future.whenComplete(() => emit(...));


  on<Event>((event, emit) async {
    await future.whenComplete(() => emit(...));


import 'package:bloc/bloc.dart';
import 'package:download_progress_with_bloc/downlaod_file.dart';
import 'package:download_progress_with_bloc/download_event.dart';
import 'package:download_progress_with_bloc/download_state.dart';
import 'package:download_progress_with_bloc/permission_handler.dart';
import 'package:download_progress_with_bloc/store_book_repo.dart';
import 'package:http/http.dart' as http;

class DownloadBloc extends Bloc<DownloadEvent, DownloadState> {
  DownloadBloc({required this.storeBookRepo}) : super(DownloadInitial()) {
  final StoreBookRepo storeBookRepo;

  Future<void> close() {
    return super.close();

  Future<void> onStarted(
      DownloadStarted event, Emitter<DownloadState> emit) async {
    print('here bitch');
    try {
      await PermissionHandler.requestStoragePermission();
      response = await downloadFile();
      totalSize = response!.contentLength ?? 0;
      emit(DownloadInProgress(progress: received, totalSize: totalSize));
      response?.stream.asBroadcastStream().listen((value) async {
        received += value.length;
        add(DownloadProgressed(progress: received));
        print('received value is $received');
        () async {
          await storeBookRepo
                bookTitle: 'thisisthefile',
              .then((value) => emit(DownloadCompleted()));
          // emit(DownloadCompleted());
    } catch (e) {
      print('the error is $e');
      emit(DownlaodFailed(errorMessage: '$e'));

  void onProgressed(DownloadProgressed event, Emitter<DownloadState> emit) {
    emit(DownloadInProgress(progress: event.progress, totalSize: totalSize));

  http.StreamedResponse? response;
  // StreamController? _controller;
  int received = 0;
  List<int> bytes = [];
  int totalSize = 0;