MutableStateFlow
interface MutableStateFlow<T> :
StateFlow<T>,
MutableSharedFlow<T>
(source)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.
Properties
abstract var value: T The current value of this state flow. |
Inherited Properties
The number of subscribers (active collectors) to this shared flow. |
Functions
Atomically compares the current value with expect and sets it to update if it is equal to expect.
The result is |
Inherited Functions
Emits a value to this shared flow, suspending on buffer overflow if the shared flow was created with the default BufferOverflow.SUSPEND strategy. |
|
abstract fun resetReplayCache(): Unit Resets the replayCache of this shared flow to an empty state. New subscribers will be receiving only the values that were emitted after this call, while old subscribers will still be receiving previously buffered values. To reset a shared flow to an initial value, emit the value after this call. |
|
Tries to emit a value to this shared flow without suspending. It returns |
Extension Properties
val FlowCollector<*>. |
|
val FlowCollector<*>. |
Extension Functions
fun <T> MutableSharedFlow<T>.asSharedFlow(): SharedFlow<T> Represents this mutable shared flow as a read-only shared flow. |
|
fun <T> MutableStateFlow<T>.asStateFlow(): StateFlow<T> Represents this mutable state flow as a read-only state flow. |
|
fun <T> Flow<T>. Creates a broadcast coroutine that collects the given flow. |
|
fun <T> Flow<T>.buffer( Buffers flow emissions via channel of a specified capacity and runs collector in a separate coroutine. |
|
fun FlowCollector<*>. |
|
Catches exceptions in the flow completion and calls a specified action with the caught exception. This operator is transparent to exceptions that occur in downstream flow and does not catch exceptions that are thrown to cancel the flow. fun <T> SharedFlow<T>. |
|
Terminal flow operator that collects the given flow but ignores all emitted values. If any exception occurs during collect or in the provided flow, this exception is rethrown from this method. Terminal flow operator that collects the given flow with a provided action. If any exception occurs during collect or in the provided flow, this exception is rethrown from this method. |
|
Terminal flow operator that collects the given flow with a provided action that takes the index of an element (zero-based) and the element. If any exception occurs during collect or in the provided flow, this exception is rethrown from this method. |
|
Terminal flow operator that collects the given flow with a provided action. The crucial difference from collect is that when the original flow emits a new value then the action block for the previous value is cancelled. |
|
Returns a Flow whose values are generated with transform function by combining the most recently emitted values by each flow. |
|
fun <T1, T2, T3, R> Flow<T1>. |
|
fun <T1, T2, R> Flow<T1>.combineTransform( Returns a Flow whose values are generated by transform function that process the most recently emitted values by each flow. |
|
Returns the number of elements matching the given predicate. |
|
Returns a flow that mirrors the original flow, but filters out values that are followed by the newer values within the given timeout. The latest value is always emitted. Returns a flow that mirrors the original flow, but filters out values that are followed by the newer values within the given timeout. The latest value is always emitted. |
|
Returns flow where all subsequent repetitions of the same value are filtered out, when compared with each other via the provided areEquivalent function. |
|
Returns flow where all subsequent repetitions of the same key are filtered out, where key is extracted with keySelector function. |
|
Returns a flow that ignores first count elements. Throws IllegalArgumentException if count is negative. |
|
Returns a flow containing all elements except first elements that satisfy the given predicate. |
|
suspend fun <T> FlowCollector<T>.emitAll( Emits all elements from the given channel to this flow collector and cancels (consumes)
the channel afterwards. If you need to iterate over the channel without consuming it,
a regular suspend fun <T> FlowCollector<T>.emitAll(flow: Flow<T>): Unit Collects all the values from the given flow and emits them to the collector.
It is a shorthand for |
|
Returns a flow containing only values of the original flow that matches the given predicate. |
|
Returns a flow containing only values that are instances of specified type R. |
|
Returns a flow containing only values of the original flow that do not match the given predicate. |
|
Returns a flow containing only values of the original flow that are not null. |
|
The terminal operator that returns the first element emitted by the flow and then cancels flow’s collection. Throws NoSuchElementException if the flow was empty. The terminal operator that returns the first element emitted by the flow matching the given predicate and then cancels flow’s collection. Throws NoSuchElementException if the flow has not contained elements matching the predicate. |
|
The terminal operator that returns the first element emitted by the flow and then cancels flow’s collection.
Returns The terminal operator that returns the first element emitted by the flow matching the given predicate and then cancels flow’s collection.
Returns |
|
Transforms elements emitted by the original flow by applying transform, that returns another flow, and then concatenating and flattening these flows. |
|
Returns a flow that switches to a new flow produced by transform function every time the original flow emits a value.
When the original flow emits a new value, the previous flow produced by |
|
fun <T, R> Flow<T>.flatMapMerge( Transforms elements emitted by the original flow by applying transform, that returns another flow, and then merging and flattening these flows. |
|
fun <T, R> Flow<T>. The operator that changes the context where all transformations applied to the given flow within a builder are executed. This operator is context preserving and does not affect the context of the preceding and subsequent operations. |
|
Accumulates value starting with initial value and applying operation current accumulator value and each element |
|
fun <T> Flow<T>.launchIn(scope: CoroutineScope): Job Terminal flow operator that launches the collection of the given flow in the scope.
It is a shorthand for |
|
Returns a flow containing the results of applying the given transform function to each value of the original flow. |
|
Returns a flow that emits elements from the original flow transformed by transform function. When the original flow emits a new value, computation of the transform block for previous value is cancelled. |
|
Returns a flow that contains only non-null results of applying the given transform function to each value of the original flow. |
|
fun <T> Flow<T>.onCompletion( Returns a flow that invokes the given action after the flow is completed or cancelled, passing the cancellation exception or failure as cause parameter of action. |
|
Returns a flow that invokes the given action before each value of the upstream flow is emitted downstream. |
|
Invokes the given action when this flow completes without emitting any elements.
The receiver of the action is FlowCollector, so |
|
Returns a flow that invokes the given action before this flow starts to be collected. |
|
fun <T> SharedFlow<T>.onSubscription( Returns a flow that invokes the given action after this shared flow starts to be collected (after the subscription is registered). |
|
fun <T> Flow<T>.produceIn( Creates a produce coroutine that collects the given flow. |
|
Accumulates value starting with the first element and applying operation to current accumulator value and each element. Throws NoSuchElementException if flow was empty. |
|
fun <T> Flow<T>.retryWhen( Retries collection of the given flow when an exception occurs in the upstream flow and the
predicate returns true. The predicate also receives an fun <T> SharedFlow<T>. |
|
Reduces the given flow with operation, emitting every intermediate result, including initial value. The first element is taken as initial value for operation accumulator. This operator has a sibling with initial value – scan. |
|
Returns a flow that emits only the latest value emitted by the original flow during the given sampling period. Returns a flow that emits only the latest value emitted by the original flow during the given sampling period. |
|
Folds the given flow with operation, emitting every intermediate result, including initial value. Note that initial value should be immutable (or should not be mutated) as it is shared between different collectors. For example: |
|
fun <T> Flow<T>.shareIn( Converts a cold Flow into a hot SharedFlow that is started in the given coroutine scope, sharing emissions from a single running instance of the upstream flow with multiple downstream subscribers, and replaying a specified number of replay values to new subscribers. See the SharedFlow documentation for the general concepts of shared flows. |
|
The terminal operator that awaits for one and only one value to be emitted. Throws NoSuchElementException for empty flow and IllegalStateException for flow that contains more than one element. |
|
The terminal operator that awaits for one and only one value to be emitted.
Returns the single value or |
|
fun <T> Flow<T>.stateIn( Converts a cold Flow into a hot StateFlow that is started in the given coroutine scope, sharing the most recently emitted value from a single running instance of the upstream flow with multiple downstream subscribers. See the StateFlow documentation for the general concepts of state flows. suspend fun <T> Flow<T>.stateIn( Starts the upstream flow in a given scope, suspends until the first value is emitted, and returns a hot StateFlow of future emissions, sharing the most recently emitted value from this running instance of the upstream flow with multiple downstream subscribers. See the StateFlow documentation for the general concepts of state flows. |
|
Returns a flow that contains first count elements. When count elements are consumed, the original flow is cancelled. Throws IllegalArgumentException if count is not positive. |
|
Returns a flow that contains first elements satisfying the given predicate. |
|
Collects given flow into a destination |
|
suspend fun <T> SharedFlow<T>. Collects given flow into a destination |
|
suspend fun <T> SharedFlow<T>. Collects given flow into a destination |
|
Applies transform function to each value of the given flow. |
|
fun <T, R> Flow<T>.transformLatest( Returns a flow that produces element by transform function every time the original flow emits a value.
When the original flow emits a new value, the previous |
|
fun <T, R> Flow<T>.transformWhile( Applies transform function to each value of the given flow while this
function returns |
|
fun <T> Flow<T>.withIndex(): Flow<IndexedValue<T>> Returns a flow that wraps each element into IndexedValue, containing value and its index (starting from zero). |
|
Zips values from the current flow ( |