Receiving from a .fold "emit was called after an event handler completed normally."

188 views Asked by At

Since I found the solution hidden of my problem in the comments, I am putting it here in a more prominent place:

Problem: I received the above error after awaiting some function within a Dart .fold method.

_failureOrProperty.fold(
          (failure) async => emit(state.copyWith(
              status: () => DataTransStatus.failure,
          (data) async {
            final List<int>? taskTypeCnts = await getTasksCount(_calledId!);
//            final List<int>? taskTypeCnts=[1,1];
            emit(state.copyWith(
                status: () => DataTransStatus.success,
                noOfMandatory: () => taskTypeCnts?[0],
                noOfOptional: () => taskTypeCnts?[1]
            ));
          }

Using the line in the comment, everything works fine, awaiting getTasksCount() does not.

1

There are 1 answers

1
w461 On

The simple solution is amending the first line with await

await _failureOrProperty.fold(. // PLACING await IN FRONT OF THE .fold
          (failure) async => emit(state.copyWith(
              status: () => DataTransStatus.failure,
          (data) async {
            final List<int>? taskTypeCnts = await getTasksCount(_calledId!);
//            final List<int>? taskTypeCnts=[1,1];
            emit(state.copyWith(
                status: () => DataTransStatus.success,
                noOfMandatory: () => taskTypeCnts?[0],
                noOfOptional: () => taskTypeCnts?[1]
            ));
          }