I have the following situation:
There's my repository class:
import com.mikhailovskii.timesapp.util.Result import kotlinx.coroutines.delay import kotlinx.coroutines.flow.flow class LoginRepository { fun fetchUser() = flow { emit(Result.Loading) delay(1000) emit(Result.Success((0..20).random())) } }
There's ViewModel class:
import androidx.lifecycle.LiveData import androidx.lifecycle.ViewModel import com.mikhailovskii.timesapp.util.Result class LoginViewModel() : ViewModel() { private val loginRepository = LoginRepository() private val a = loginRepository.fetchUser() val user: LiveData<Result<Int>> get() = loginRepository.fetchUser().asLiveData() }
And there's Result class:
sealed class Result<out R> { data class Success<out T>(val data: T) : Result<T>() object Loading : Result<Nothing>() object Error : Result<Nothing>() }
So, when I try to convert the Repository's Flow to the LiveData with the help of asLiveData
method, asLiveData
is underlined and studio writes that it's an unresolved reference. But I cannot understand why does it happens, as repository returns Flow. So, how what's the problem and how can I solve it?
I think you are missing the LiveData dependency.
def lifecycle_version = "2.2.0" // LiveData implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
Documentation
You are missing a dependency:
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.2.0"
(or any higher version)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With