collectAsState

Functions summary

State<R>
@Composable
<T : R, R : Any?> Flow<T>.collectAsState(
    initial: R,
    context: CoroutineContext,
    mutationPolicy: SnapshotMutationPolicy<R>
)

Collects values from this Flow and represents its latest value via State.

Cmn
State<T>
@Composable
<T : Any?> StateFlow<T>.collectAsState(
    context: CoroutineContext,
    mutationPolicy: SnapshotMutationPolicy<T>
)

Collects values from this StateFlow and represents its latest value via State.

Cmn

Functions

Flow.collectAsState

@Composable
fun <T : R, R : Any?> Flow<T>.collectAsState(
    initial: R,
    context: CoroutineContext = EmptyCoroutineContext,
    mutationPolicy: SnapshotMutationPolicy<R> = structuralEqualityPolicy()
): 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.

Optionally, the context that the flow is collected in and the mutationPolicy that is used to report and merge changes in the returned state can be customized. If the context or Flow changes, the same state will be returned, the previous collection will be canceled, and the provided Flow will start collection in the new context. Changes to the mutationPolicy after the state has been created are ignored.

import androidx.compose.material.Text
import androidx.compose.runtime.collectAsState

val value: String by flow.collectAsState("initial")
Text("Value is $value")
Parameters
initial: R

the value of the state will have until the first flow value is emitted.

context: CoroutineContext = EmptyCoroutineContext

CoroutineContext to use for collecting.

mutationPolicy: SnapshotMutationPolicy<R> = structuralEqualityPolicy()

A policy used to control how changes are handled in the returned state.

StateFlow.collectAsState

@Composable
fun <T : Any?> StateFlow<T>.collectAsState(
    context: CoroutineContext = EmptyCoroutineContext,
    mutationPolicy: SnapshotMutationPolicy<T> = structuralEqualityPolicy()
): 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.

Optionally, the context that the flow is collected in and the mutationPolicy that is used to report and merge changes in the returned state can be customized. If the context or StateFlow changes, the same state will be returned, the previous collection will be canceled, and the provided Flow will start collection in the new context. Changes to the mutationPolicy after the state has been created are ignored.

import androidx.compose.material.Text
import androidx.compose.runtime.collectAsState

val value: String by stateFlow.collectAsState()
Text("Value is $value")
Parameters
context: CoroutineContext = EmptyCoroutineContext

CoroutineContext to use for collecting.

mutationPolicy: SnapshotMutationPolicy<T> = structuralEqualityPolicy()

A policy used to control how changes are handled in the returned state.