---
title: "collectAsState"
description: "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."
type: "composable"
---

<div class='type'>Composable Function</div>


<a id='references'></a>

<div class='sourceset sourceset-common'>Common</div>


```kotlin
@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. |




<div class='sourceset sourceset-common'>Common</div>


```kotlin
@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. |





## Code Examples
### FlowWithInitialSample
```kotlin
@Composable
fun FlowWithInitialSample(flow: Flow<String>) {
    val value: String by flow.collectAsState("initial")
    Text("Value is $value")
}
```
### StateFlowSample
```kotlin
@Composable
fun StateFlowSample(stateFlow: StateFlow<String>) {
    val value: String by stateFlow.collectAsState()
    Text("Value is $value")
}
```

