Home > Net >  Unresolved reference: Transformations
Unresolved reference: Transformations

Time:04-27

I'm trying to follow the android documentation to implement Transformations in my project but I run into this problem Unresolved reference: Transformations. I don't know why my project can't see the Transformations class.

I'm using Kotlin version 1.5.21' and here is my code

class MyViewModel(private val repository: PostalCodeRepository) : ViewModel() {
    private val addressInput = MutableLiveData<String>()
    val postalCode: LiveData<String> = Transformations.switchMap(addressInput) {
            address -> repository.getPostCode(address) }


    private fun setInput(address: String) {
        addressInput.value = address
    }
}

Any guidance is really appreciated.

CodePudding user response:

Make sure to import

import androidx.lifecycle.Transformations

If the import gives an Unresolved reference error, add the following dependency to your build.gradle file

dependencies {
    ...
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.4.1"
}
  • Related