Using List with Cubit

Saracha Tongkumpunt
1 min readJun 26, 2022
Photo by Marc A on Unsplash

When I was trying to manage a state of List with Cubit — a simpler version of BLoC — I found that the state was not update as soon as the event was triggered. It looked like ReactJS that if a state is an object, it won’t update immediately. You need to put a callback or spread the object. But I didn’t know how to deal with it in Dart.

Let’s start with my Staff model below. (I will omit the code in the Flutter part.)

Then I tried to create a Cubit of list of Staff.

It seemed work but the UI didn’t redraw as soon as the event occurred. I had to tap on where it should redraw to make it happen.

Fortunately, I found this question on stack overflow. In summary, I had to create another class that had a list of Staff, and re-write the Cubit.

And this worked as expected!

The code looks like it spreads the list of Staff into a new list of a new class, so the state updates and triggers the UI redrawing.

--

--