Is it possible to get `MutableStateFlow<S>` ...
# coroutines
r
Is it possible to get
MutableStateFlow<S>
from
MutableStateFlow<S?>
?
i
filterNotNull()
+
stateIn()
. You need to pick either the version that specifies exactly what
initialState
you want when your
MutableStateFlow<S?>
is null or use the version that suspends until a non-null value becomes available
Remember the whole contract of
StateFlow
is that it always has a value
r
But this way I can only get
StateFlow<S>
and I would like mutable version.
d
Make a new one and copy the value over.
i
Ah, then you can probably follow the advice of this thread (doing the
filterNotNull()
) https://kotlinlang.slack.com/archives/C1CFAFJSK/p1612919750413900?thread_ts=1612919750.413900&amp;cid=C1CFAFJSK
r
@Dominaezzz Thx, but I'm not sure a new stateflow is what I'm looking for. I would like to have a kind of "subview" to the orignal stateflow. So I could both change the original state value (of course with non-null values only) and get/collect the state value (non-nullable, with a given initial value in place of original nulls). I would like all collectors of the original stateflow to still get all the values. I'm thinking about a wrapper over new
StateFlow<S>
I get from the
stateIn()
, which maps setter call to the original stateflow, but it would require implementing
MutableStateFlow
interface, which is probably bad idea.
Probably I should think about it once again in the morning 😉
z
So you’re trying to map in both directions? I don’t think anything like that exists, but you could always just implement the interfaces yourself and make sure to satisfy all the contracts.