UiState
data class UiState<T : Any>(val data: T, val loading: Boolean = false, val error: OneTimeEvent<Throwable?> = OneTimeEvent(null))
Wrapper class representing the complete state of a UI screen.
This is the standard state container used across all ViewModels in the application. It combines screen data with loading and error states in a single, immutable structure.
Key Features
Type-safe data: Generic type parameter ensures compile-time safety
Loading state: Boolean flag for showing/hiding loading indicators
One-time errors: Errors are delivered as events that are handled only once
Usage in ViewModel
@HiltViewModel
class FeatureViewModel @Inject constructor(
private val repository: FeatureRepository
) : ViewModel() {
private val _uiState = MutableStateFlow(UiState(FeatureScreenData()))
val uiState = _uiState.asStateFlow()
}Content copied to clipboard
State Updates
Use the provided extension functions to update state:
updateState for synchronous updates
updateStateWith for async operations returning new data
updateWith for async operations returning Unit
Parameters
T
The type of the screen data. Must be a non-nullable type.
See also
Constructors
Link copied to clipboard
constructor(data: T, loading: Boolean = false, error: OneTimeEvent<Throwable?> = OneTimeEvent(null))