produceState

fun <T> produceState(initialValue: T, producer: suspend ProduceStateScope<T>.() -> Unit): State<T>

Return an observable snapshotState that produces values over time without a defined data source.

producer is launched when produceState enters the composition and is cancelled when produceState leaves the composition. producer should use ProduceStateScope.value to set new values on the returned State.

The returned State conflates values; no change will be observable if ProduceStateScope.value is used to set a value that is equal to its old value, and observers may only see the latest value if several values are set in rapid succession.

produceState may be used to observe either suspending or non-suspending sources of external data, for example:

Samples

androidx.compose.runtime.samples.ProduceStateandroidx.compose.runtime.samples.ProduceStateAwaitDispose
fun <T> produceState(initialValue: T, subject: Any?, producer: suspend ProduceStateScope<T>.() -> Unit): State<T>

Return an observable snapshotState that produces values over time from subject.

producer is launched when produceState enters the composition and is cancelled when produceState leaves the composition. If subject changes, a running producer will be cancelled and re-launched for the new source. producer should use ProduceStateScope.value to set new values on the returned State.

The returned State conflates values; no change will be observable if ProduceStateScope.value is used to set a value that is equal to its old value, and observers may only see the latest value if several values are set in rapid succession.

produceState may be used to observe either suspending or non-suspending sources of external data, for example:

Samples

androidx.compose.runtime.samples.ProduceStateandroidx.compose.runtime.samples.ProduceStateAwaitDispose
fun <T> produceState(initialValue: T, subject1: Any?, subject2: Any?, producer: suspend ProduceStateScope<T>.() -> Unit): State<T>

Return an observable snapshotState that produces values over time from subject1 and subject2.

producer is launched when produceState enters the composition and is cancelled when produceState leaves the composition. If subject1 or subject2 change, a running producer will be cancelled and re-launched for the new source. producer should use ProduceStateScope.value to set new values on the returned State.

The returned State conflates values; no change will be observable if ProduceStateScope.value is used to set a value that is equal to its old value, and observers may only see the latest value if several values are set in rapid succession.

produceState may be used to observe either suspending or non-suspending sources of external data, for example:

Samples

androidx.compose.runtime.samples.ProduceStateandroidx.compose.runtime.samples.ProduceStateAwaitDispose
fun <T> produceState(initialValue: T, subject1: Any?, subject2: Any?, subject3: Any?, producer: suspend ProduceStateScope<T>.() -> Unit): State<T>

Return an observable snapshotState that produces values over time from subject1, subject2 and subject3.

producer is launched when produceState enters the composition and is cancelled when produceState leaves the composition. If subject1, subject2 or subject3 change, a running producer will be cancelled and re-launched for the new source. [producer should use ProduceStateScope.value to set new values on the returned State.

The returned State conflates values; no change will be observable if ProduceStateScope.value is used to set a value that is equal to its old value, and observers may only see the latest value if several values are set in rapid succession.

produceState may be used to observe either suspending or non-suspending sources of external data, for example:

Samples

androidx.compose.runtime.samples.ProduceStateandroidx.compose.runtime.samples.ProduceStateAwaitDispose
fun <T> produceState(initialValue: T, vararg subjects: Any?, producer: suspend ProduceStateScope<T>.() -> Unit): State<T>

Return an observable snapshotState that produces values over time from subjects.

producer is launched when produceState enters the composition and is cancelled when produceState leaves the composition. If subjects change, a running producer will be cancelled and re-launched for the new source. producer should use ProduceStateScope.value to set new values on the returned State.

The returned State conflates values; no change will be observable if ProduceStateScope.value is used to set a value that is equal to its old value, and observers may only see the latest value if several values are set in rapid succession.

produceState may be used to observe either suspending or non-suspending sources of external data, for example:

Samples

androidx.compose.runtime.samples.ProduceStateandroidx.compose.runtime.samples.ProduceStateAwaitDispose