flutter_bloc (emit was called after an event handler completed normally
Viewed 5 times
So, I was working with Django rest framework creating my APIs and all. And i wanted to check if I can get the response from the backend. So, I was able to get the response. But I saw some issues on the debug console. Please help me figure out the problem. This is what I received on the Debug Console.
Restarted application in 1,314ms.
I/flutter ( 1751): {"token":"0d15f2d45f11869174395d623c066080bd2ade52"}
E/flutter ( 1751): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: 'package:bloc/src/bloc.dart': Failed assertion: line 232 pos 7: '!_isCompleted':
E/flutter ( 1751):
E/flutter ( 1751):
E/flutter ( 1751): emit was called after an event handler completed normally.
E/flutter ( 1751): This is usually due to an unawaited future in an event handler.
E/flutter ( 1751): Please make sure to await all asynchronous operations with event handlers
E/flutter ( 1751): and use emit.isDone after asynchronous operations before calling emit() to
E/flutter ( 1751): ensure the event handler has not completed.
E/flutter ( 1751):
E/flutter ( 1751): **BAD**
E/flutter ( 1751): on<Event>((event, emit) {
E/flutter ( 1751): future.whenComplete(() => emit(...));
E/flutter ( 1751): });
E/flutter ( 1751):
E/flutter ( 1751): **GOOD**
E/flutter ( 1751): on<Event>((event, emit) async {
E/flutter ( 1751): await future.whenComplete(() => emit(...));
E/flutter ( 1751): });
E/flutter ( 1751):
E/flutter ( 1751): #0 _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:46:39)
E/flutter ( 1751): #1 _AssertionError._throwNew (dart:core-patch/errors_patch.dart:36:5)
E/flutter ( 1751): #2 _Emitter.call
E/flutter ( 1751): #3 new LoginFormBloc.<anonymous closure>.<anonymous closure>
E/flutter ( 1751): <asynchronous suspension>
E/flutter ( 1751):
Reloaded 1 of 746 libraries in 1,055ms.
Here's my source code:
class LoginFormBloc extends Bloc<LoginFormEvent, LoginFormState> {
LoginFormBloc(this._getAuthInfo) : super(LoginFormState.initial()) {
on<LoginFormEvent>(
(event, emit) {
event.map(
emailChanged: (e) {
emit(
state.copyWith(
emailAddress: EmailAddress(e.emailStr),
authFailureOrSuccessOption: none(),
),
);
},
passwordChanged: (e) {
emit(
state.copyWith(
password: Password(e.passwordStr),
authFailureOrSuccessOption: none(),
),
);
},
loggedIn: (e) async {
final isEmailValid = state.emailAddress.value.isRight();
final isPasswordValid = state.password.value.isRight();
Either<AuthFailure, TokenValue>? loginFailureOrSuccess;
if (isEmailValid && isPasswordValid) {
emit(
state.copyWith(
isSubmitting: true,
authFailureOrSuccessOption: none(),
),
);
loginFailureOrSuccess = await _getAuthInfo(
Params(
emailAddress: state.emailAddress,
password: state.password,
),
);
}
emit(
state.copyWith(
isSubmitting: false,
showErrorMessages: true,
authFailureOrSuccessOption: optionOf(loginFailureOrSuccess),
),
);
},
);
},
);
}
final GetAuthToken _getAuthInfo;
}
The error is pointed on 2nd emit inside loggedIn event handler.