Flutter BLoC Not Emitting State

Created At: 2023-06-20 19:23:06 Updated At: 2023-06-20 19:54:09

If you want to flutter_bloc package you might see that sometimes, it does not emit states. The frustation becomes more when you see that you have triggered or added a certain event, but state is not there.

Since, I have been working BLoC, Riverpod and Getx pretty much all the time, I would say something about BLoC not updating state or emit state.

The reasons are easy to sort out. You need to be careful about the below three reasons to check out

1.Emitting the same state

You may be emitting the same state again and again. So second or third time, you try to emit the same state, it would not. 

After first time, if you add the event with same data for emitting state it won't just work. 

Make sure each time you call an add method for same event, you are passing different data

2. Too many events together

Too many events together? What does it mean?

From the picture, we see that we are trying to trigger two events subsequently one after another. The events on line 26, would not be able to state update.

This is because, it happens too fast. If you add, multiple events, the subsequent events need to delayed a little bit.

Two ways to work on this, 

  1. add different syntax or do something different. Try to execute other task and it would work.
  2. or put the subsequent event inside Future.delay() function

In general I use the second approach. Let's see the same code for the second approach for delay trigger events

3. No related states

The third reason is that you are just not emitting the right state. You may or may not have the state. Make sure you have the right state and event in your BLoC.

BLoC Resources

1. Complete BLoC and Cubit App

2. Complete BLoC E-commerce App

3. BLoC Task Management App

 

Comment

Add Reviews