-2

I have a state

val selectedRationList: MutableStateFlow<ArrayList<Ration>?> = MutableStateFlow(null)`

I updated this like this

uiState.selectedRationList.update { data }

The problem here is that when the list becomes empty, it recomposes the UI only once and doesn't recompose whether you try to add new items to the list or remove them.

But when it became empty I did it like this

if (data.isEmpty()) {
    state.selectedRationList.value = null
} else {
    state.selectedRationList.value = data
}

It recomposes as many times I add new items or remove them. Why?

Abhimanyu
  • 11,351
  • 7
  • 51
  • 121
  • You have to use `mutableStateListOf()` as given [here](https://stackoverflow.com/a/69932964/9636037) – Abhimanyu Jun 13 '23 at 13:42
  • Does this answer your question? [correct way to handle mutable state of list of data in jetpack compose](https://stackoverflow.com/questions/69932411/correct-way-to-handle-mutable-state-of-list-of-data-in-jetpack-compose) – Abhimanyu Jun 13 '23 at 13:43

1 Answers1

0

MutableStateFlow only recomposes when the value changes, but list operation does not change the value.

Use mutableStateListOf<Ration>().

For example:

val selectedRationList = mutableStateListOf<Ration>()

selectedRationList.add(Ration())
selectedRationList.removeLast()
Gargantua
  • 21
  • 2