collectDataStoreState

fun <T> Flow<T>.collectDataStoreState(initial: () -> T, logTag: String, onErrorReset: (MutableState<T>) -> Unit = {}): State<T>(source)

Collects a Flow as a State specifically designed for DataStore operations within Compose.

This function monitors the flow's lifecycle and provides specialized error handling. If the flow completes due to an exception (excluding CancellationException), it logs the error, resets the state to its initial value, and triggers the onErrorReset callback to allow for custom recovery logic.

Return

A State object representing the current value of the DataStore flow.

Parameters

initial

A provider for the initial value and the fallback value in case of an error.

logTag

The tag used for logging error messages.

onErrorReset

A callback invoked when an error occurs, providing access to the MutableState for manual intervention or UI synchronization.

Type Parameters

T

The type of data being collected.