I have a `MutableStateFlow<Map<..., ...>&...
# coroutines
I have a
MutableStateFlow<Map<..., ...>>
what’s the most idiomatic way to mutate the map and update the
? seems like I need to make a copy of the current value and then update it? something like this seems verbose and awkward
Copy code
stateFlow.value = stateFlow.value.toMutableMap().apply { … modify …}
i could do this
Copy code
inline fun <K, V> MutableStateFlow<MutableMap<K, V>>.mutate(mutation: (MutableMap<K, V>) -> Unit) {
    val newValue = value.toMutableMap()
    value = newValue
and usage would just be
Copy code
stateFlow.mutate {
  ... modify ...
It's the same problem as replacing any observable collection with
, for example when using a
on Android. Inexplicably, I never saw this addressed anywhere. I'm not an expert, though.
I suggest
MutableStateFlow<Map<K, V>>
. You can update it with just
stateFlow.value += item
or something similar. If you need more complicated mutations, then i think you are stuck with more complicated code.