输入“状态<列表<用户>?"没有方法“getValue(Nothing?, KProperty<*>)",因此它不能作为委托 [英] Type 'State<List<User>?>' has no method 'getValue(Nothing?, KProperty<*>)' and thus it cannot serve as a delegate
本文介绍了输入“状态<列表<用户>?"没有方法“getValue(Nothing?, KProperty<*>)",因此它不能作为委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 jetpack compose 中的 observeAsState 从 LiveData 获取值,但我收到一个奇怪的错误
I'm trying to get a value from LiveData with observeAsState in jetpack compose, but I get a weird error
输入状态<列表?>"没有方法 'getValue(Nothing?,KProperty<*>)',因此它不能作为委托
Type 'State<List?>' has no method 'getValue(Nothing?, KProperty<*>)' and thus it cannot serve as a delegate
代码
@Composable
fun UserScreen(userViewModel:UserViewModel){
val items: List<User> by userViewModel.fetchUserList.observeAsState()
UserList(userList = items)
}
class UserViewModel: ViewModel() {
private val dataSource = UserDataSource()
val fetchUserList = liveData {
emit(dataSource.dummyUserList)
}
}
推荐答案
如果您收到未定义observeAsState 或 getValue 的编译器错误,请确保您有以下导入:
If you get a compiler error that observeAsState or getValue are not defined make sure you have the following imports:
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
此信息来自在 Jetpack Compose 中使用状态一个>"代码实验室.
This information is from Step #4 in the "Using State in Jetpack Compose" codelab.
这篇关于输入“状态<列表<用户>?"没有方法“getValue(Nothing?, KProperty<*>)",因此它不能作为委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文