I have a strange issue. ViewModel get data from db with a viewModelScope... after update a list in my uiStage model.. is a MutableStateFlow... when i put a brakpoint i see change while without brakpoint ui doesn/t change.
Gioele Dev
12/18/2022, 9:30 AM
Copy code
viewModelScope.launch {
var confirmedList = getItemsConfirmedByScodeUseCase.invoke()
var groups = confirmedList.groupBy { it }
var modules = uiState.value.modules
modules.forEach {
it.badge = ""
}
groups.forEach { op, list ->
modules.forEach {
if (it.operationId == op) {
it.badge = list.size.toString()
}
}
}
uiState.value = uiState.value.copy(
modules = modules
)
}
Gioele Dev
12/18/2022, 9:30 AM
modules is a list inside my
Copy code
val uiState = MutableStateFlow<DashboardState>(DashboardState(false))
i
Ian Lake
12/18/2022, 4:35 PM
It looks like you are just editing the list items in place, rather than creating a new list instance with your updated data. Your copy() isn't actually doing anything useful here since
uiState.value.modules
is being changed out from underneath it, meaning your previous emission and new emission are
.equals()
to one another, which is why you don't get any state change in Compose
Ian Lake
12/18/2022, 4:36 PM
Generally, you'll want to avoid mutable items and mutable lists entirely when using StateFlow
g
Gioele Dev
12/18/2022, 4:46 PM
thanks
Gioele Dev
12/18/2022, 4:47 PM
i moved calculate badges before create Module list so.. i avoid use "`uiState.value.modules`"