Response
A generic class that holds a value with its loading status. Can be used to represent any operation that can be in one of the following states: Loading, Success, Error.
fun example() { // With data val responseWithData: Response
// Empty success
val emptyResponse: Response = Response.SuccessEmpty
// Or using convenience constructor
val anotherEmptyResponse: Response = Response.success()
// Handling all cases
when (response) {
is Response.Success -> println("Success with data: ${response.data}")
is Response.SuccessEmpty -> println("Success with no data")
is Response.Error -> println("Error: ${response.exception.message}")
is Response.Loading -> println("Loading...")
}
// Using handlers
response
.onSuccess { data -> println("Success: $data") }
.onSuccessEmpty { println("Success with no data") }
.onError { error -> println("Error: ${error.message}") }
.onLoading { println("Loading...") } Content copied to clipboard
}
Inheritors
Types
Properties
Link copied to clipboard
Returns true if this response represents a successful state (either Success or SuccessEmpty).
Functions
Link copied to clipboard
Returns the data if this is a Success response, or the provided default value otherwise.
Link copied to clipboard
Executes the given action if this is a SuccessEmpty response.