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

Javier

01/21/2021, 10:01 PM
Is it possible to
filterNotNull
a
MutableStateFlow
and keep returning a
StateFlow
instead of a
Flow
? It is forcing me to add an
stateIn
to retransform the
Flow
into
StateFlow
g

Guillermo Alcantara

01/21/2021, 10:05 PM
I thought states couldn't be nullable
j

Javier

01/21/2021, 10:12 PM
They can be nullable but I think there is no way to filter the emission of null states without
stateIn
i

Ian Lake

01/21/2021, 10:12 PM
The whole point of
StateFlow
is that it has always has a
value
. If your upstream
MutableStateFlow
only emits
null
, then
filterNotNull
never emits anything. That's why you need to use
stateIn()
to either set an
initialValue
(so that your
StateFlow
actually has a
value
) or use the suspend
stateIn
that suspends until the first value is emitted
👍 2
👍🏼 1
j

Javier

01/21/2021, 10:14 PM
Thank you Ian 🙂 The last words about
stateIn
is what I was imagining
4 Views