我正在尝试遵循 Kotlin 中 ViewModels 的官方 Android 指南。我从字面上复制粘贴了最简单的官方示例,但语法似乎是非法的。
本节导致问题:
private val users: MutableLiveData<List<User>> by lazy {
MutableLiveData().also {
loadUsers()
}
}
预览给了我这个错误:
Property delegate must have a 'getValue(DashViewModel, KProperty*>)' method. None of the following functions is suitable.
如果我想启动应用程序,我会收到此错误:
Type inference failed: Not enough information to infer parameter T in constructor MutableLiveData<T : Any!>()
Please specify it explicitly.
我不明白这两个错误和其他具有相同错误的问题似乎是由不同的东西引起的。我的猜测是MutableLiveData().also导致问题的原因,但我不知道为什么。考虑到这是一个官方的例子,这很奇怪。