in my Flutter application using Cubit Bloc as the state manager. I am facing a difficulty while setting up the business logic of my project.
It seems that when emitting events in an asynchronous function, only the last emission is taken into account. In my case, the last emission is emit(TicketStateNotTreated).
Here is the relevant code:
Future<void> fetchTicketsList() async {
try {
emit(const TicketStateLoading());
List<TicketModel> ticketsList = (await repository.fetchTicketsList())!;
for (var ticket in ticketsList) {
if (ticket.status == true) {
treatedTickets.add(ticket);
} else {
notTreatedTickets.add(ticket);
}
}
emit(
TicketStateTreated(
tickets: treatedTickets,
total: treatedTickets.length,
status: true,
),
);
emit(
TicketStateNotTreated(
tickets: notTreatedTickets,
total: notTreatedTickets.length,
status: false,
),
);
return;
} catch (e) {
emit(TicketFailure(message: e.toString()));
return;
}
}
Can you help me solve this issue? I want the TicketStateTreated and TicketStateNotTreated emissions to all be taken into account. Thanks in advance!
.
Why do you need two state with almost same fields? Just create some TickerLoaded with both lists and emit it once
Yeah thanks. I solved my problem
Обсуждают сегодня