[英]How to observe livedata in compose?
I need to observe change in list which I am fetching from firebase我需要观察我从 firebase 获取的列表中的变化
viewModel.courseList.observe(viewLifeCycleOwner,{
})
But I'm not able to use viewLifeCycleOwner
.但我无法使用
viewLifeCycleOwner
。
class MyViewModel: ViewModel() {
private var _courseList = MutableLiveData<Whatever>()
var courseList: LiveData<List<Whatever>> = _courseList
}
@Composable
fun MyComposable() {
val list = myViewModel.courseList.observeAsState().value
}
Demo app has plenty of examples: https://github.com/JohannBlake/Jetmagic演示应用程序有很多示例: https : //github.com/JohannBlake/Jetmagic
use subscribe
or ko.computed for detect any change in observable variable like使用
subscribe
或ko.computed来检测可观察变量的任何变化,例如
viewModel.courseList = ko.observable()
viewModel.courseList.subscribe((changedvalue)=>{
//to do code
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.