https://kotlinlang.org logo
#coroutines
Title
# coroutines
r

Robert Jaros

02/13/2021, 1:28 AM
Is it possible to get
MutableStateFlow<S>
from
MutableStateFlow<S?>
?
i

Ian Lake

02/13/2021, 1:40 AM
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

Robert Jaros

02/13/2021, 1:46 AM
But this way I can only get
StateFlow<S>
and I would like mutable version.
d

Dominaezzz

02/13/2021, 1:57 AM
Make a new one and copy the value over.
i

Ian Lake

02/13/2021, 2:01 AM
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

Robert Jaros

02/13/2021, 2:14 AM
@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

Zach Klippenstein (he/him) [MOD]

02/13/2021, 5:37 AM
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.
5 Views