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

andylamax

07/08/2020, 4:16 AM
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

Tim Malseed

07/08/2020, 4:19 AM
It’s still a
MutableStateFlow
, you’ve just lost the type inference. You could cast your resulting
Flow<T>
as a
MutableStateFlow<>
z

Zach Klippenstein (he/him) [MOD]

07/08/2020, 4:25 AM
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

Tim Malseed

07/08/2020, 4:27 AM
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

andylamax

07/08/2020, 5:00 AM
Thank you all for your responses
very helpful
z

Zach Klippenstein (he/him) [MOD]

07/08/2020, 1:48 PM
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
6 Views