collectAsState
@Composable
public fun <T> 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.
Parameters
| context | CoroutineContext to use for collecting. |
| mutationPolicy | A policy used to control how changes are handled in the returned state. |
collectAsState
@Composable
public fun <T : R, R> 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.
Parameters
| initial | the value of the state will have until the first flow value is emitted. |
| context | CoroutineContext to use for collecting. |
| mutationPolicy | A policy used to control how changes are handled in the returned state. |
collectAsState
@Deprecated(
"Use the overload with a SnapshotMutationPolicy parameter",
level = DeprecationLevel.HIDDEN,
)
@Composable
public 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.
Parameters
| context | CoroutineContext to use for collecting. |
collectAsState
@Deprecated(
"Use the overload with a SnapshotMutationPolicy parameter",
level = DeprecationLevel.HIDDEN,
)
@Composable
public 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.
Parameters
| initial | the value of the state will have until the first flow value is emitted. |
| context | CoroutineContext to use for collecting. |