flutter -bloc – how to resolve Failed Assertion : !_isComplete

Issue

I am stuck at this exception .

I am trying to create a Number Trivia app based on course on you tube

basically i’m handling errors and exceptions with Either class from dartz library;

on<ConcreteNumberTriviaGottenEvent>((event, emit) async {
  
  emit(NumberTriviaLoadingState());
  final number =
      InputConverter.convertStringToUnsignedInteger(event.numberString);
  print(number);
  
  number.fold((numberFailure) => emit(Error(message: failMessage(numberFailure))), (numberInt) async {

    final either = await concreteTriviaUseCase.exec(numberInt);
    either!.fold((serverFailure) =>  emit(Error(message: failMessage(serverFailure))),
    (numberTrivia) => emit(NumberTriviaLoadedState(numberTrivia: numberTrivia)));
    
    
  });
});

so basically the string number comes from event and get passed to a static method from class InputConversion which return an Either<Failure,int> . i execute a fold on the returned value emitting Error state in the case of Left aka Failure and for Right i get the NumberTrivia from api using returned int . api also returns an Either which i fold on again but it throws the following exception


E/flutter ( 6066): emit was called after an event handler completed normally.
E/flutter ( 6066): This is usually due to an unawaited future in an event handler.
E/flutter ( 6066): Please make sure to await all asynchronous operations with event handlers
E/flutter ( 6066): and use emit.isDone after asynchronous operations before calling emit() to
E/flutter ( 6066): ensure the event handler has not completed.

Solution

The solution is to place an await before number.fold:

await number.fold((numberFailure) => emit(Error(message: failMessage(numberFailure))), (numberInt) async {

I got stuck in the same spot on the same tutorial and this dicussion helped me figure out what’s wrong: https://github.com/felangel/bloc/issues/2784

Answered By – James Parsons

Answer Checked By – Robin (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *