asFlow

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

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


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

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>(source)

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


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

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


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

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


fun <T> Array<T>.asFlow(): Flow<T>(source)

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.


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.


Creates a flow that produces values from the range.


Represents this shared flow and its subtypes as a plain flow, hiding its hot flow characteristics.

Unlike simple upcasting, the returned flow prevents casting back to the original hot flow type, ensuring that implementation details remain encapsulated.

Example:

class Repository {
private val _updates = MutableStateFlow("initial")

// Exposes Flow interface without leaking MutableStateFlow implementation
val updates: Flow<String> = _updates.asFlow()
}

// Usage
val flow = repository.updates
val mutableStateFlow = flow as? MutableStateFlow // null - cast prevented
val stateFlow = flow as? StateFlow // null - cast prevented