# collectAsState

Common/AllAndroid/JVMNative/C/iOSJavaScript

## Functions summary

|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State<R>` | `@https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composable <T : R, R : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html<T>.https://developer.android.com/reference/kotlin/androidx/compose/runtime/collectAsState.composable#(kotlinx.coroutines.flow.Flow).collectAsState(kotlin.Any,kotlin.coroutines.CoroutineContext,androidx.compose.runtime.SnapshotMutationPolicy)( initial: R, context: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html, mutationPolicy: https://developer.android.com/reference/kotlin/androidx/compose/runtime/SnapshotMutationPolicy<R> )` Collects values from this `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html` and represents its latest value via `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State`. | Cmn |
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State<T>` | `@https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composable <T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-state-flow/index.html<T>.https://developer.android.com/reference/kotlin/androidx/compose/runtime/collectAsState.composable#(kotlinx.coroutines.flow.StateFlow).collectAsState(kotlin.coroutines.CoroutineContext,androidx.compose.runtime.SnapshotMutationPolicy)( context: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html, mutationPolicy: https://developer.android.com/reference/kotlin/androidx/compose/runtime/SnapshotMutationPolicy<T> )` Collects values from this `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-state-flow/index.html` and represents its latest value via `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State`. | Cmn |

## Functions

### Flow.collectAsState

Cmn Artifact: [androidx.compose.runtime:runtime](https://developer.android.com/jetpack/androidx/releases/compose-runtime) [View Source](https://cs.android.com/search?q=file:androidx/compose/runtime/SnapshotFlow.kt+function:collectAsState) Added in [1.13.0-rc01](https://developer.android.com/jetpack/androidx/releases/compose-runtime#1.13.0-rc01)

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

Collects values from this `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html` and represents its latest value via `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State`. Every time there would be new value posted into the `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html` the returned `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State` will be updated causing recomposition of every `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State#value()` usage.

Optionally, the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#(kotlinx.coroutines.flow.Flow).collectAsState(kotlin.Any,kotlin.coroutines.CoroutineContext,androidx.compose.runtime.SnapshotMutationPolicy)` that the flow is collected in and the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#(kotlinx.coroutines.flow.Flow).collectAsState(kotlin.Any,kotlin.coroutines.CoroutineContext,androidx.compose.runtime.SnapshotMutationPolicy)` that is used to report and merge changes in the returned state can be customized. If the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#(kotlinx.coroutines.flow.Flow).collectAsState(kotlin.Any,kotlin.coroutines.CoroutineContext,androidx.compose.runtime.SnapshotMutationPolicy)` 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 `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#(kotlinx.coroutines.flow.Flow).collectAsState(kotlin.Any,kotlin.coroutines.CoroutineContext,androidx.compose.runtime.SnapshotMutationPolicy)` after the state has been created are ignored.

```kotlin
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: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html = EmptyCoroutineContext` | `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html` to use for collecting. |
| `mutationPolicy: https://developer.android.com/reference/kotlin/androidx/compose/runtime/SnapshotMutationPolicy<R> = structuralEqualityPolicy()` | A policy used to control how changes are handled in the returned state. |

### StateFlow.collectAsState

Cmn Artifact: [androidx.compose.runtime:runtime](https://developer.android.com/jetpack/androidx/releases/compose-runtime) [View Source](https://cs.android.com/search?q=file:androidx/compose/runtime/SnapshotFlow.kt+function:collectAsState) Added in [1.13.0-rc01](https://developer.android.com/jetpack/androidx/releases/compose-runtime#1.13.0-rc01)

```
@Composable
fun <T : Any?> StateFlow<T>.collectAsState(
    context: CoroutineContext = EmptyCoroutineContext,
    mutationPolicy: SnapshotMutationPolicy<T> = structuralEqualityPolicy()
): State<T>
```

Collects values from this `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-state-flow/index.html` and represents its latest value via `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State`. The `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-state-flow/value.html` is used as an initial value. Every time there would be new value posted into the `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-state-flow/index.html` the returned `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State` will be updated causing recomposition of every `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State#value()` usage.

Optionally, the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#(kotlinx.coroutines.flow.StateFlow).collectAsState(kotlin.coroutines.CoroutineContext,androidx.compose.runtime.SnapshotMutationPolicy)` that the flow is collected in and the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#(kotlinx.coroutines.flow.StateFlow).collectAsState(kotlin.coroutines.CoroutineContext,androidx.compose.runtime.SnapshotMutationPolicy)` that is used to report and merge changes in the returned state can be customized. If the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#(kotlinx.coroutines.flow.StateFlow).collectAsState(kotlin.coroutines.CoroutineContext,androidx.compose.runtime.SnapshotMutationPolicy)` 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 `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#(kotlinx.coroutines.flow.StateFlow).collectAsState(kotlin.coroutines.CoroutineContext,androidx.compose.runtime.SnapshotMutationPolicy)` after the state has been created are ignored.

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

val value: String by stateFlow.collectAsState()
Text("Value is $value")
```

| Parameters |
|---|---|
| `context: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html = EmptyCoroutineContext` | `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html` to use for collecting. |
| `mutationPolicy: https://developer.android.com/reference/kotlin/androidx/compose/runtime/SnapshotMutationPolicy<T> = structuralEqualityPolicy()` | A policy used to control how changes are handled in the returned state. |