我正在尝试按照 android 文档在我的项目中实现转换,但我遇到了这个问题Unresolved reference: Transformations。我不知道为什么我的项目看不到 Transformations 类。
我正在使用 Kotlin 1.5.21 版,这是我的代码
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
}
}
非常感谢任何指导。
确保导入
import androidx.lifecycle.Transformations
如果导入Unresolved reference
出错,请将以下依赖项添加到您的build.gradle
文件中
dependencies {
...
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.4.1"
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句