{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

ProduceStateScope

public interface ProduceStateScope<T extends Object> extends MutableState


Receiver scope for use with produceState.

Summary

Public methods

abstract @NonNull Void
awaitDispose(@NonNull Function0<Unit> onDispose)

Await the disposal of this producer whether it left the composition, the source changed, or an error occurred.

Inherited methods

From androidx.compose.runtime.MutableState
abstract @NonNull T
abstract @NonNull Function1<@NonNull T, Unit>
abstract @NonNull T
abstract void
setValue(@NonNull T value)

Public methods

awaitDispose

abstract @NonNull Void awaitDispose(@NonNull Function0<Unit> onDispose)

Await the disposal of this producer whether it left the composition, the source changed, or an error occurred. Always runs onDispose before resuming.

This method is useful when configuring callback-based state producers that do not suspend, for example:

import androidx.compose.runtime.produceState

val currentPerson by produceState<Person?>(null, viewModel) {
    val disposable = viewModel.registerPersonObserver { person ->
        value = person
    }

    awaitDispose {
        disposable.dispose()
    }
}