MutableStateFlow

interface MutableStateFlow<T> : StateFlow<T> , MutableSharedFlow<T>

A mutable StateFlow that provides a setter for value. An instance of MutableStateFlow with the given initial value can be created using MutableStateFlow(value) constructor function.

See the StateFlow documentation for details on state flows.

Not stable for inheritance

The MutableStateFlow interface is not stable for inheritance in 3rd party libraries, as new methods might be added to this interface in the future, but is stable for use. Use the MutableStateFlow() constructor function to create an implementation.

Functions

compareAndSet
Link copied to clipboard
abstract fun compareAndSet(expect: T, update: T): Boolean

Atomically compares the current value with expect and sets it to update if it is equal to expect. The result is true if the value was set to update and false otherwise.

Properties

value
Link copied to clipboard
abstract override var value: T

The current value of this state flow.

Extensions

asStateFlow
Link copied to clipboard
fun <T> MutableStateFlow<T>.asStateFlow(): StateFlow<T>

Represents this mutable state flow as a read-only state flow.

getAndUpdate
Link copied to clipboard
inline fun <T> MutableStateFlow<T>.getAndUpdate(function: (T) -> T): T

Updates the MutableStateFlow.value atomically using the specified function of its value, and returns its prior value.

update
Link copied to clipboard
inline fun <T> MutableStateFlow<T>.update(function: (T) -> T)

Updates the MutableStateFlow.value atomically using the specified function of its value.

updateAndGet
Link copied to clipboard
inline fun <T> MutableStateFlow<T>.updateAndGet(function: (T) -> T): T

Updates the MutableStateFlow.value atomically using the specified function of its value, and returns the new value.

Sources

common source
Link copied to clipboard