https://kotlinlang.org logo
Title
a

AmrJyniat

11/15/2021, 3:44 PM
Is there an equivalent to
LiveData builder
in flow?
j

Joffrey

11/15/2021, 3:48 PM
I'm not very familiar with
LiveData
(I haven't done Android development in years), but there is a builder for flows as well, such as:
flow {
    // suspend calls are OK here
    delay(100)
    emit(42)
}
Now
LiveData
seems to be more an equivalent for a
StateFlow
than a regular cold
Flow
that this builder would create. But you can always use
stateIn()
to convert your cold flow into a state flow (that will require an initial value though).
a

Adam Powell

11/15/2021, 5:42 PM
the
liveData {}
builder was modeled after kotlinx.coroutines'
flow {}
and
channelFlow {}
builders, so yes. 🙂