livedata转换成compose 的state,使用需要添加compose livedata的依赖
@Composable
fun StatisticsPage() {
val dataList by viewModel.dataList.observeAsState(listOf())
}
// compose livedata相关
implementation "androidx.compose.runtime:runtime:$compose_version"
implementation "androidx.compose.runtime:runtime-livedata:$compose_version"
compose组件中获取context
@Composable
fun StatisticsPage() {
val context = LocalContext.current
}
compose组件中获取viewmodel
@Composable
fun StatisticsPage() {
val viewModel: StatisticsViewModel = viewModel()
}
compose 中使用协程
@Composable
fun StatisticsPage() {
val scope = rememberCoroutineScope()
}