produceState

Functions summary

State<T>
@Composable
<T : Any?> produceState(initialValue: T, producer: suspend ProduceStateScope<T>.() -> Unit)

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

Cmn
State<T>
@Composable
<T : Any?> produceState(
    initialValue: T,
    mutationPolicy: SnapshotMutationPolicy<T>,
    producer: suspend ProduceStateScope<T>.() -> Unit
)

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

Cmn
State<T>
@Composable
<T : Any?> produceState(initialValue: T, key1: Any?, producer: suspend ProduceStateScope<T>.() -> Unit)

Return an observable snapshot State that produces values over time from key1.

Cmn
State<T>
@Composable
<T : Any?> produceState(
    initialValue: T,
    vararg keys: Any?,
    producer: suspend ProduceStateScope<T>.() -> Unit
)

Return an observable snapshot State that produces values over time from keys.

Cmn
State<T>
@Composable
<T : Any?> produceState(
    initialValue: T,
    key1: Any?,
    mutationPolicy: SnapshotMutationPolicy<T>,
    producer: suspend ProduceStateScope<T>.() -> Unit
)

Return an observable snapshot State that produces values over time from key1.

Cmn
State<T>
@Composable
<T : Any?> produceState(
    initialValue: T,
    key1: Any?,
    key2: Any?,
    producer: suspend ProduceStateScope<T>.() -> Unit
)

Return an observable snapshot State that produces values over time from key1 and key2.

Cmn
State<T>
@Composable
<T : Any?> produceState(
    initialValue: T,
    vararg keys: Any?,
    mutationPolicy: SnapshotMutationPolicy<T>,
    producer: suspend ProduceStateScope<T>.() -> Unit
)

Return an observable snapshot State that produces values over time from keys.

Cmn
State<T>
@Composable
<T : Any?> produceState(
    initialValue: T,
    key1: Any?,
    key2: Any?,
    mutationPolicy: SnapshotMutationPolicy<T>,
    producer: suspend ProduceStateScope<T>.() -> Unit
)

Return an observable snapshot State that produces values over time from key1 and key2.

Cmn
State<T>
@Composable
<T : Any?> produceState(
    initialValue: T,
    key1: Any?,
    key2: Any?,
    key3: Any?,
    producer: suspend ProduceStateScope<T>.() -> Unit
)

Return an observable snapshot State that produces values over time from key1, key2 and key3.

Cmn
State<T>
@Composable
<T : Any?> produceState(
    initialValue: T,
    key1: Any?,
    key2: Any?,
    key3: Any?,
    mutationPolicy: SnapshotMutationPolicy<T>,
    producer: suspend ProduceStateScope<T>.() -> Unit
)

Return an observable snapshot State that produces values over time from key1, key2 and key3.

Cmn

Functions

produceState

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

Return an observable snapshot State 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. This can be changed by providing a SnapshotMutationPolicy.

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

import androidx.compose.foundation.layout.Column
import androidx.compose.runtime.produceState

val uiState by
    produceState<UiState<List<Person>>>(UiState.Loading, viewModel) {
        viewModel.people.map { UiState.Data(it) }.collect { value = it }
    }

when (val state = uiState) {
    is UiState.Loading -> Text("Loading...")
    is UiState.Data ->
        Column {
            for (person in state.data) {
                Text("Hello, ${person.name}")
            }
        }
}
import androidx.compose.runtime.produceState

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

        awaitDispose { disposable.dispose() }
    }
Parameters
initialValue: T

The value that the returned state will initially contain

producer: suspend ProduceStateScope<T>.() -> Unit

A suspending lambda that defines what values are emitted to the returned state

produceState

@Composable
fun <T : Any?> produceState(
    initialValue: T,
    mutationPolicy: SnapshotMutationPolicy<T>,
    producer: suspend ProduceStateScope<T>.() -> Unit
): State<T>

Return an observable snapshot State 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 given mutationPolicy is used to control how changes are reported and merged in the returned state. If not specified, the default behavior is structuralEqualityPolicy, which conflates values that are equal to each other. Note observers may only see the latest value if several values are set in rapid succession. This is especially true when reading the returned state in composition, as composition executes with the latest values from a snapshot rather than composing once with each intermediate value of a state.

The mutationPolicy is only used when initializing the underlying state object. If the mutationPolicy changes after creating the state, the state and the producer are unaffected and will continue collecting in the previously returned state with the original SnapshotMutationPolicy.

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

import androidx.compose.foundation.layout.Column
import androidx.compose.runtime.produceState

val uiState by
    produceState<UiState<List<Person>>>(UiState.Loading, viewModel) {
        viewModel.people.map { UiState.Data(it) }.collect { value = it }
    }

when (val state = uiState) {
    is UiState.Loading -> Text("Loading...")
    is UiState.Data ->
        Column {
            for (person in state.data) {
                Text("Hello, ${person.name}")
            }
        }
}
import androidx.compose.runtime.produceState

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

        awaitDispose { disposable.dispose() }
    }
Parameters
initialValue: T

The value that the returned state will initially contain

mutationPolicy: SnapshotMutationPolicy<T>

A policy used to control how changes are handled in the returned state

producer: suspend ProduceStateScope<T>.() -> Unit

A suspending lambda that defines what values are emitted to the returned state

produceState

@Composable
fun <T : Any?> produceState(initialValue: T, key1: Any?, producer: suspend ProduceStateScope<T>.() -> Unit): State<T>

Return an observable snapshot State that produces values over time from key1.

producer is launched when produceState enters the composition and is cancelled when produceState leaves the composition. If key1 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. This can be changed by providing a SnapshotMutationPolicy.

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

import androidx.compose.foundation.layout.Column
import androidx.compose.runtime.produceState

val uiState by
    produceState<UiState<List<Person>>>(UiState.Loading, viewModel) {
        viewModel.people.map { UiState.Data(it) }.collect { value = it }
    }

when (val state = uiState) {
    is UiState.Loading -> Text("Loading...")
    is UiState.Data ->
        Column {
            for (person in state.data) {
                Text("Hello, ${person.name}")
            }
        }
}
import androidx.compose.runtime.produceState

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

        awaitDispose { disposable.dispose() }
    }
Parameters
initialValue: T

The value that the returned state will initially contain

key1: Any?

A key that, when changed, will restart the producer lambda

producer: suspend ProduceStateScope<T>.() -> Unit

A suspending lambda that defines what values are emitted to the returned state

produceState

@Composable
fun <T : Any?> produceState(
    initialValue: T,
    vararg keys: Any?,
    producer: suspend ProduceStateScope<T>.() -> Unit
): State<T>

Return an observable snapshot State that produces values over time from keys.

producer is launched when produceState enters the composition and is cancelled when produceState leaves the composition. If keys 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. This can be changed by providing a SnapshotMutationPolicy.

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

import androidx.compose.foundation.layout.Column
import androidx.compose.runtime.produceState

val uiState by
    produceState<UiState<List<Person>>>(UiState.Loading, viewModel) {
        viewModel.people.map { UiState.Data(it) }.collect { value = it }
    }

when (val state = uiState) {
    is UiState.Loading -> Text("Loading...")
    is UiState.Data ->
        Column {
            for (person in state.data) {
                Text("Hello, ${person.name}")
            }
        }
}
import androidx.compose.runtime.produceState

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

        awaitDispose { disposable.dispose() }
    }
Parameters
initialValue: T

The value that the returned state will initially contain

vararg keys: Any?

A list of keys that, when changed, will restart the producer lambda

producer: suspend ProduceStateScope<T>.() -> Unit

A suspending lambda that defines what values are emitted to the returned state

produceState

@Composable
fun <T : Any?> produceState(
    initialValue: T,
    key1: Any?,
    mutationPolicy: SnapshotMutationPolicy<T>,
    producer: suspend ProduceStateScope<T>.() -> Unit
): State<T>

Return an observable snapshot State that produces values over time from key1.

producer is launched when produceState enters the composition and is cancelled when produceState leaves the composition. If key1 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 given mutationPolicy is used to control how changes are reported and merged in the returned state. If not specified, the default behavior is structuralEqualityPolicy, which conflates values that are equal to each other. Note observers may only see the latest value if several values are set in rapid succession. This is especially true when reading the returned state in composition, as composition executes with the latest values from a snapshot rather than composing once with each intermediate value of a state.

Changes to the mutationPolicy and initialValue are ignored.

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

import androidx.compose.foundation.layout.Column
import androidx.compose.runtime.produceState

val uiState by
    produceState<UiState<List<Person>>>(UiState.Loading, viewModel) {
        viewModel.people.map { UiState.Data(it) }.collect { value = it }
    }

when (val state = uiState) {
    is UiState.Loading -> Text("Loading...")
    is UiState.Data ->
        Column {
            for (person in state.data) {
                Text("Hello, ${person.name}")
            }
        }
}
import androidx.compose.runtime.produceState

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

        awaitDispose { disposable.dispose() }
    }
Parameters
initialValue: T

The value that the returned state will initially contain

key1: Any?

A key that, when changed, will restart the producer lambda

mutationPolicy: SnapshotMutationPolicy<T>

A policy used to control how changes are handled in the returned state

producer: suspend ProduceStateScope<T>.() -> Unit

A suspending lambda that defines what values are emitted to the returned state

produceState

@Composable
fun <T : Any?> produceState(
    initialValue: T,
    key1: Any?,
    key2: Any?,
    producer: suspend ProduceStateScope<T>.() -> Unit
): State<T>

Return an observable snapshot State that produces values over time from key1 and key2.

producer is launched when produceState enters the composition and is cancelled when produceState leaves the composition. If key1 or key2 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. This can be changed by providing a SnapshotMutationPolicy.

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

import androidx.compose.foundation.layout.Column
import androidx.compose.runtime.produceState

val uiState by
    produceState<UiState<List<Person>>>(UiState.Loading, viewModel) {
        viewModel.people.map { UiState.Data(it) }.collect { value = it }
    }

when (val state = uiState) {
    is UiState.Loading -> Text("Loading...")
    is UiState.Data ->
        Column {
            for (person in state.data) {
                Text("Hello, ${person.name}")
            }
        }
}
import androidx.compose.runtime.produceState

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

        awaitDispose { disposable.dispose() }
    }
Parameters
initialValue: T

The value that the returned state will initially contain

key1: Any?

A key that, when changed, will restart the producer lambda

key2: Any?

A key that, when changed, will restart the producer lambda

producer: suspend ProduceStateScope<T>.() -> Unit

A suspending lambda that defines what values are emitted to the returned state

produceState

@Composable
fun <T : Any?> produceState(
    initialValue: T,
    vararg keys: Any?,
    mutationPolicy: SnapshotMutationPolicy<T>,
    producer: suspend ProduceStateScope<T>.() -> Unit
): State<T>

Return an observable snapshot State that produces values over time from keys.

producer is launched when produceState enters the composition and is cancelled when produceState leaves the composition. If keys 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 given mutationPolicy is used to control how changes are reported and merged in the returned state. If not specified, the default behavior is structuralEqualityPolicy, which conflates values that are equal to each other. Note observers may only see the latest value if several values are set in rapid succession. This is especially true when reading the returned state in composition, as composition executes with the latest values from a snapshot rather than composing once with each intermediate value of a state.

The mutationPolicy is only used when initializing the underlying state object. If the mutationPolicy changes after creating the state, the state and the producer are unaffected and will continue collecting in the previously returned state with the original SnapshotMutationPolicy.

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

import androidx.compose.foundation.layout.Column
import androidx.compose.runtime.produceState

val uiState by
    produceState<UiState<List<Person>>>(UiState.Loading, viewModel) {
        viewModel.people.map { UiState.Data(it) }.collect { value = it }
    }

when (val state = uiState) {
    is UiState.Loading -> Text("Loading...")
    is UiState.Data ->
        Column {
            for (person in state.data) {
                Text("Hello, ${person.name}")
            }
        }
}
import androidx.compose.runtime.produceState

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

        awaitDispose { disposable.dispose() }
    }
Parameters
initialValue: T

The value that the returned state will initially contain

vararg keys: Any?

A list of keys that, when changed, will restart the producer lambda

mutationPolicy: SnapshotMutationPolicy<T>

A policy used to control how changes are handled in the returned state

producer: suspend ProduceStateScope<T>.() -> Unit

A suspending lambda that defines what values are emitted to the returned state

produceState

@Composable
fun <T : Any?> produceState(
    initialValue: T,
    key1: Any?,
    key2: Any?,
    mutationPolicy: SnapshotMutationPolicy<T>,
    producer: suspend ProduceStateScope<T>.() -> Unit
): State<T>

Return an observable snapshot State that produces values over time from key1 and key2.

producer is launched when produceState enters the composition and is cancelled when produceState leaves the composition. If key1 or key2 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 given mutationPolicy is used to control how changes are reported and merged in the returned state. If not specified, the default behavior is structuralEqualityPolicy, which conflates values that are equal to each other. Note observers may only see the latest value if several values are set in rapid succession. This is especially true when reading the returned state in composition, as composition executes with the latest values from a snapshot rather than composing once with each intermediate value of a state.

The mutationPolicy is only used when initializing the underlying state object. If the mutationPolicy changes after creating the state, the state and the producer are unaffected and will continue collecting in the previously returned state with the original SnapshotMutationPolicy.

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

import androidx.compose.foundation.layout.Column
import androidx.compose.runtime.produceState

val uiState by
    produceState<UiState<List<Person>>>(UiState.Loading, viewModel) {
        viewModel.people.map { UiState.Data(it) }.collect { value = it }
    }

when (val state = uiState) {
    is UiState.Loading -> Text("Loading...")
    is UiState.Data ->
        Column {
            for (person in state.data) {
                Text("Hello, ${person.name}")
            }
        }
}
import androidx.compose.runtime.produceState

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

        awaitDispose { disposable.dispose() }
    }
Parameters
initialValue: T

The value that the returned state will initially contain

key1: Any?

A key that, when changed, will restart the producer lambda

key2: Any?

A key that, when changed, will restart the producer lambda

mutationPolicy: SnapshotMutationPolicy<T>

A policy used to control how changes are handled in the returned state

producer: suspend ProduceStateScope<T>.() -> Unit

A suspending lambda that defines what values are emitted to the returned state

produceState

@Composable
fun <T : Any?> produceState(
    initialValue: T,
    key1: Any?,
    key2: Any?,
    key3: Any?,
    producer: suspend ProduceStateScope<T>.() -> Unit
): State<T>

Return an observable snapshot State that produces values over time from key1, key2 and key3.

producer is launched when produceState enters the composition and is cancelled when produceState leaves the composition. If key1, key2 or key3 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. This can be changed by providing a SnapshotMutationPolicy.

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

import androidx.compose.foundation.layout.Column
import androidx.compose.runtime.produceState

val uiState by
    produceState<UiState<List<Person>>>(UiState.Loading, viewModel) {
        viewModel.people.map { UiState.Data(it) }.collect { value = it }
    }

when (val state = uiState) {
    is UiState.Loading -> Text("Loading...")
    is UiState.Data ->
        Column {
            for (person in state.data) {
                Text("Hello, ${person.name}")
            }
        }
}
import androidx.compose.runtime.produceState

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

        awaitDispose { disposable.dispose() }
    }
Parameters
initialValue: T

The value that the returned state will initially contain

key1: Any?

A key that, when changed, will restart the producer lambda

key2: Any?

A key that, when changed, will restart the producer lambda

key3: Any?

A key that, when changed, will restart the producer lambda

producer: suspend ProduceStateScope<T>.() -> Unit

A suspending lambda that defines what values are emitted to the returned state

produceState

@Composable
fun <T : Any?> produceState(
    initialValue: T,
    key1: Any?,
    key2: Any?,
    key3: Any?,
    mutationPolicy: SnapshotMutationPolicy<T>,
    producer: suspend ProduceStateScope<T>.() -> Unit
): State<T>

Return an observable snapshot State that produces values over time from key1, key2 and key3.

producer is launched when produceState enters the composition and is cancelled when produceState leaves the composition. If key1, key2 or key3 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 given mutationPolicy is used to control how changes are reported and merged in the returned state. If not specified, the default behavior is structuralEqualityPolicy, which conflates values that are equal to each other. Note observers may only see the latest value if several values are set in rapid succession. This is especially true when reading the returned state in composition, as composition executes with the latest values from a snapshot rather than composing once with each intermediate value of a state.

The mutationPolicy is only used when initializing the underlying state object. If the mutationPolicy changes after creating the state, the state and the producer are unaffected and will continue collecting in the previously returned state with the original SnapshotMutationPolicy.

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

import androidx.compose.foundation.layout.Column
import androidx.compose.runtime.produceState

val uiState by
    produceState<UiState<List<Person>>>(UiState.Loading, viewModel) {
        viewModel.people.map { UiState.Data(it) }.collect { value = it }
    }

when (val state = uiState) {
    is UiState.Loading -> Text("Loading...")
    is UiState.Data ->
        Column {
            for (person in state.data) {
                Text("Hello, ${person.name}")
            }
        }
}
import androidx.compose.runtime.produceState

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

        awaitDispose { disposable.dispose() }
    }
Parameters
initialValue: T

The value that the returned state will initially contain

key1: Any?

A key that, when changed, will restart the producer lambda

key2: Any?

A key that, when changed, will restart the producer lambda

key3: Any?

A key that, when changed, will restart the producer lambda

mutationPolicy: SnapshotMutationPolicy<T>

A policy used to control how changes are handled in the returned state

producer: suspend ProduceStateScope<T>.() -> Unit

A suspending lambda that defines what values are emitted to the returned state