asFlow

fun <T> () -> T.asFlow(): Flow<T>

Creates a cold flow that produces a single value from the given functional type.


fun <T> suspend () -> T.asFlow(): Flow<T>

Creates a cold flow that produces a single value from the given functional type.

Example of usage:

suspend fun remoteCall(): R = ...
fun remoteCallFlow(): Flow<R> = ::remoteCall.asFlow()

fun <T> Iterable<T>.asFlow(): Flow<T>

Creates a cold flow that produces values from the given iterable.


fun <T> Iterator<T>.asFlow(): Flow<T>

Creates a cold flow that produces values from the given iterator.


fun <T> Sequence<T>.asFlow(): Flow<T>

Creates a cold flow that produces values from the given sequence.


fun <T> Array<T>.asFlow(): Flow<T>
fun LongArray.asFlow(): Flow<Long>

Creates a cold flow that produces values from the given array. The flow being cold means that the array components are read every time a terminal operator is applied to the resulting flow.


fun IntArray.asFlow(): Flow<Int>

Creates a cold flow that produces values from the array. The flow being cold means that the array components are read every time a terminal operator is applied to the resulting flow.


fun IntRange.asFlow(): Flow<Int>
fun LongRange.asFlow(): Flow<Long>

Creates a flow that produces values from the range.


fun <T> BroadcastChannel<T>.asFlow(): Flow<T>

Represents the given broadcast channel as a hot flow. Every flow collector will trigger a new broadcast channel subscription.

Cancellation semantics

  1. Flow consumer is cancelled when the original channel is cancelled.

  2. Flow consumer completes normally when the original channel completes (~is closed) normally.

  3. If the flow consumer fails with an exception, subscription is cancelled.