Google は、黒人コミュニティに対する人種平等の促進に取り組んでいます。取り組みを見る

androidx.compose.runtime.livedata

Extension functions summary

For LiveData
State<T?>

Starts observing this LiveData and represents its values via State.

State<R>
LiveData<T>.observeAsState(initial: R)

Starts observing this LiveData and represents its values via State.

Extension functions

observeAsState

@Composable fun <T> 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, 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 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")