Response

sealed class Response<out T>

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 = Response.Success("Hello")

// 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...") }

}

Inheritors

Types

Link copied to clipboard
data class Error(val exception: Throwable) : Response<Nothing>
Link copied to clipboard
data object Loading : Response<Nothing>
Link copied to clipboard
data class Success<T>(val data: T) : Response<T>
Link copied to clipboard
data object SuccessEmpty : Response<Nothing>

Properties

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard

Functions

Link copied to clipboard
fun getOrDefault(defaultValue: @UnsafeVariance T): T
Link copied to clipboard
fun getOrNull(): T?
Link copied to clipboard
inline fun <T, R> Response<T>.map(transform: (T) -> R): Response<R>
Link copied to clipboard
fun onError(action: (Throwable) -> Unit): Response<T>
Link copied to clipboard
fun onLoading(action: () -> Unit): Response<T>
Link copied to clipboard
suspend fun onSuccess(action: suspend (T) -> Unit): Response<T>
Link copied to clipboard
suspend fun onSuccessEmpty(action: suspend () -> Unit): Response<T>