I’m trying to emit event after an http request is complete with flutter bloc but from my flutter bloc code I’m getting the error below. How do I fix this
_startConversation(
Emitter<MediboardAiBotState> emit, StartConversationEvent event) async {
print("Start ...(1)");
ChatMessage.fromJson({
'role': 'user',
'content': event.prompt,
'threadId': '',
});
conversationRetrievalState = ConversationRetrievalState.requesting;
emit(const ConversationLoadingState(
state: ConversationRetrievalState.requesting));
// try {
print("Start ...(2)");
// List<ChatMessage> response = await
_apiService
.startConversation(event.prompt)
.then((List<ChatMessage> response) async {
chatConversation = response;
threadId =
response[0].threadId?.isEmpty == true ? null : response[0].threadId;
conversationRetrievalState = ConversationRetrievalState.success;
emit(const ConversationLoadingState(
state: ConversationRetrievalState.success));
emit(StartConversationState(response: response));
}).catchError((e) async {
print("Start err $e ...(3)");
conversationRetrievalState = ConversationRetrievalState.failed;
emit(const ConversationLoadingState(
state: ConversationRetrievalState.failed));
emit(const StartConversationState(response: []));
});
// } catch (e) {
// }
}
package:bloc/src/emitter.dart': Failed assertion: line 114 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. **BAD** on<Event>((event, emit) { future.whenComplete(() => emit(...)); }); **GOOD** on<Event>((event, emit) async { await future.whenComplete(() => emit(...)); });