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()
}