Gordon
08/08/2023, 12:07 PMsealed class ApiResult<out T:Any> {
data class Success<out T:Any>(val value : T) : ApiResult<T>()
data class Error(val message : String, val cause: Exception? = null) : ApiResult<Nothing>()
object Loading : ApiResult<Nothing>()
}
But when i try to use it like this:
val result by viewModel.allPositions.collectAsStateWithLifecycle(initialValue = ApiResult.Loading)
when (result) {
is ApiResult.Error -> {
item {
Text("Error: ${result.message}")
}
}
I get: Smart cast to 'ApiResult.Error' is impossible, because 'result' is a property that has open or custom gettermkrussel
08/08/2023, 12:13 PMby
and smart casting. by
makes the property a computed property and the compiler doesn't know that it will not change (I'm not sure that it won't) between the type check and the use.
Instead you will have to do val result = viewModel.allPositions.collectAsStateWithLifecycle(initialValue = ApiResult.Loading).value
Gordon
08/08/2023, 12:17 PM