[英]Expected non-nullable value for MutableLiveData
private val _users = MutableLiveData<List<User>>()
val users: LiveData<List<User>> get() = _users
fun getUsers() {
viewModelScope.launch {
_users.value = users()
}
}
suspend fun users(): List<User> {
TODO("Not implemented")
}
I get following error on _users.value = users()
我在
_users.value = users()
上收到以下错误
Expected non-nullable value.
预期的不可为空的值。 Inspection info: This check ensures that LiveData values are not null when explicitly declared as non-nullable.
检查信息:此检查确保 LiveData 值在明确声明为不可为空时不是 null。
I'm using lifecycle version 2.3.1
.我正在使用生命周期版本
2.3.1
。 The problem seems to be with suspend function users().问题似乎在于暂停 function users()。 If I remove the suspend modifier it works fine.
如果我删除挂起修饰符,它工作正常。
Just use private val _users:MutableLiveData<List<User>> = MutableLiveData()
instead of private val _users = MutableLiveData<List<User>>()
.只需使用
private val _users:MutableLiveData<List<User>> = MutableLiveData()
而不是private val _users = MutableLiveData<List<User>>()
。
Try this._users.value = users()
.试试
this._users.value = users()
。 Adding this
at the front works for me.在前面添加
this
对我有用。 Not sure why.不知道为什么。 In your case you might need
this@yourModel._users.value = users()
since you are calling it in the viewModelScope.在您的情况下,您可能需要
this@yourModel._users.value = users()
因为您在 viewModelScope 中调用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.