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 inline 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")