updateStateWith

context(viewModel: ViewModel)
inline fun <T : Any> MutableStateFlow<UiState<T>>.updateStateWith(crossinline operation: suspend T.() -> Result<T>)(source)

Launches an operation in the context ViewModel's scope and replaces data on returned success.

A call observing loading = true is dropped. The flag is set inside the launched coroutine, so multiple calls queued before it runs can overlap. Starting clears the previous error event. Returned failure preserves current data and stores its throwable in OneTimeEvent; returned success replaces data. Both paths clear loading. A successful null payload is treated as an error.

Thrown exceptions and cancellation are not caught and can leave loading true. The operation's receiver is a data snapshot read inside the coroutine; returning a copy of it can overwrite edits made while the operation was suspended. This helper returns no operation result or Job.

Parameters

operation

Suspend transform returning replacement data or a failure in Result.

Type Parameters

T

The non-null screen-data type.

See also