flutter -bloc - 如何解决断言失败:!_isComplete

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

我卡在这个异常中了。

我正在尝试根据你管上的课程创建一个数字问答应用程序

基本上我正在使用 dartz 库中的 Either class 处理错误和异常;

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)));
    
    
  });
});

所以字符串编号基本上来自事件,并从 class InputConversion 传递到静态方法,其中 return 一个 Either 。我在 returned 值上执行折叠,在左侧又名失败的情况下发出错误状态,对于右侧,我使用 returned int 从 api 获得 NumberTrivia。 api 也 returns 一个 Either 我再次折叠但它抛出以下异常


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.

解决方法是在number.fold之前放置一个await:

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

我在同一个教程的同一个地方被卡住了,这个讨论帮助我找出了问题所在:https://github.com/felangel/bloc/issues/2784