collectAsState

@ExperimentalCoroutinesApi()
inline fun <T> StateFlow<T>.collectAsState(context: CoroutineContext = EmptyCoroutineContext): State<T>

Collects values from this StateFlow and represents its latest value via State. The StateFlow.value is used as an initial value. Every time there would be new value posted into the StateFlow the returned State will be updated causing recomposition of every State.value usage.

Samples

androidx.compose.runtime.samples.StateFlowSample

Parameters

context

CoroutineContext to use for collecting.

inline fun <T : R, R> Flow<T>.collectAsState(initial: R, context: CoroutineContext = EmptyCoroutineContext): State<R>

Collects values from this Flow and represents its latest value via State. Every time there would be new value posted into the Flow the returned State will be updated causing recomposition of every State.value usage.

Samples

androidx.compose.runtime.samples.FlowWithInitialSample

Parameters

context

CoroutineContext to use for collecting.