androidx.compose.runtime.livedata

Extension functions summary

State<T?>

Starts observing this LiveData and represents its values via State.

State<R>
@Composable
<R : Any?, T : R> LiveData<T>.observeAsState(initial: R)

Starts observing this LiveData and represents its values via State.

Extension functions

observeAsState

@Composable
fun <T : Any?> LiveData<T>.observeAsState(): State<T?>

Starts observing this LiveData and represents its values via State. Every time there would be new value posted into the LiveData the returned State will be updated causing recomposition of every State.value usage.

The inner observer will automatically be removed when this composable disposes or the current LifecycleOwner moves to the Lifecycle.State.DESTROYED state.

import androidx.compose.material.Text
import androidx.compose.runtime.livedata.observeAsState

val value: String? by liveData.observeAsState()
Text("Value is $value")

observeAsState

@Composable
fun <R : Any?, T : R> LiveData<T>.observeAsState(initial: R): State<R>

Starts observing this LiveData and represents its values via State. Every time there would be new value posted into the LiveData the returned State will be updated causing recomposition of every State.value usage.

The initial value will be used only if this LiveData is not already initialized. Note that if T is a non-null type, it is your responsibility to ensure that any value you set on this LiveData is also non-null.

The inner observer will automatically be removed when this composable disposes or the current LifecycleOwner moves to the Lifecycle.State.DESTROYED state.

import androidx.compose.material.Text
import androidx.compose.runtime.livedata.observeAsState

val value: String by liveData.observeAsState("initial")
Text("Value is $value")