asOneTimeEvent
Wraps this Throwable in a OneTimeEvent for use in UI state management.
This is useful when you want to ensure errors are only shown once to the user, even if the state is re-collected (e.g., during configuration changes).
Note: The template uses UiState wrapper which already includes OneTimeEvent for errors. This function is mainly useful if you're managing errors separately or building custom state.
Usage Examples
// In a custom state class (if not using UiState)
data class MyScreenState(
val data: List<Item> = emptyList(),
val error: OneTimeEvent<Throwable?> = OneTimeEvent(null)
)
// In a ViewModel
private val _state = MutableStateFlow(MyScreenState())
fun loadData() {
viewModelScope.launch {
try {
val data = repository.getData()
_state.update { it.copy(data = data) }
} catch (e: Exception) {
_state.update { it.copy(error = e.asOneTimeEvent()) }
}
}
}
// In UI - error only shown once
val state by viewModel.state.collectAsStateWithLifecycle()
state.error.consume { error ->
// This block runs only once per error, even after rotation
showSnackbar(error.message ?: "Unknown error")
}Content copied to clipboard
Receiver
Throwable The error to wrap in a one-time event.
Return
A OneTimeEvent containing this throwable.
See also
dev.atick.core.ui.utils.UiState
For the recommended state wrapper pattern