Hi, Why on MutableLiveData value change Compose di...
# compose
a
Hi, Why on MutableLiveData value change Compose didn`t compose the UI?
Copy code
viewModel.listOfRoundFlights.observeAsState()
    .value?.let{
 // some compose function
}
a
If you're modifying a list and setting the livedata to that same list again, compose doesn't consider that a new value so it won't recompose. Emit a new list instead of mutating the contents of the livedata
Alternatively, skip the livedata entirely and use
mutableStateListOf
for your
listOfRoundFlights
in your ViewModel and you can modify the list directly and compose will notice and respond to the modifications
a
@Adam Powell i use this
Copy code
private val _listOfRoundFlights =
    MutableStateFlow<List<Pair<Pair<Int, Int>, Pair<OutboundX, OutboundX>>>>(listOf())

val listOfRoundFlights: StateFlow<List<Pair<Pair<Int, Int>, Pair<OutboundX, OutboundX>>>> =
    _listOfRoundFlights
But same problem again.
a
I think you should consider writing one or more data classes;
Pair<Pair<Int, Int>, Pair<OutboundX, OutboundX>>
is quite hard to read or make sense of 🙂
if at any point in your other code you are modifying a mutable list and expecting it to emit a new value, compose's state management does not consider it a new value since it is the same list object. This is the same regardless of whether you're importing that state from a livedata or a flow
c
Copy code
_listOfRoundFlights.update {
  it.toMutableList().apply {
    // modify your list here
  }
}
Something like the above should do