how do I map from a `(Mutable)StateFlow<T>` ...
# coroutines
a
how do I map from a
(Mutable)StateFlow<T>
to another
(Mutable)StateFlow<T>
? when I call the
map
method I map the
StateFlow<T>
to a
Flow<T>
t
It’s still a
MutableStateFlow
, you’ve just lost the type inference. You could cast your resulting
Flow<T>
as a
MutableStateFlow<>
z
No,
map
does not return a
StateFlow
, it returns a regular
Flow
. You'd need to use something like
stateIn
to convert it back.
☝️ 1
t
Oh, my mistake. I’ll refrain from answering coroutine questions for a few more weeks haha
I assumed that map just operates on the values after they’re emitting, so the underlying flow could still be a
MutableStateFlow
. But I guess that doesn’t make sense, since the Flow’s value type has changed (it emits a different type to the one it receives)
a
Thank you all for your responses
very helpful
z
The reason map can't maintain the
StateFlow
type on its own is explained by Roman in the comments here: https://github.com/Kotlin/kotlinx.coroutines/issues/2081
👍 2