Why BLoC does not work and update ui?

Created At: 2024-01-05 11:41:03 Updated At: 2024-01-07 05:33:55

Here we will see why BLoC does not update the UI even though the state has been emitted? There could be many reasons.

Here we see how we are getting the states based on the Cubit or BLoC. But the is is a bad practice most of the time, and create bugs.

In the above picture, you see we are getting the states and saving in a local variable. Build() method does not get latest based on the state changes unless you call setState() function.

You need to listen to states change inside BlocBuilder, BlocConsumer or BlocListener. When you listen to that, you will always get the value of state changes.

If you don't do like that, even if you detect that states are emitted(), but your ui won't update since build() method in nature is static. 

It has to be triggered explicitly be told to rebuild ui. Make sure you have BlocConsumer or, BlocBuilder or BlocListener.

You need to retreive and use the states like in the picture


Add Reviews