And*_*lov 10 android kotlin android-jetpack-compose
我想从 ViewModel 中的 Jetpack Compose 更改 ModalBottomSheet 的状态。
我所做的是,我有一个 ViewModel,如下所示:
class MyViewModel() {
@ExperimentalMaterialApi
val bottomSheetDialogState = ModalBottomSheetState(
initialValue = ModalBottomSheetValue.Hidden
)
fun showBottomSheet() {
viewModelScope.launch {
bottomSheetDialogState.show()
}
}
}
Run Code Online (Sandbox Code Playgroud)
和内部片段:
@ExperimentalMaterialApi
@Composable
fun ShowUnableToChangeDialog() {
ModalBottomSheetLayout(
sheetState = viewModel.bottomSheetDialogState,
sheetShape = RoundedCornerShape(topStart = 10.dp, topEnd = 10.dp),
sheetBackgroundColor = Color.White,
sheetContent = {
Box(modifier = Modifier.defaultMinSize(minHeight = 1.dp)) {
Column {
}
}
}
) { }
}
Run Code Online (Sandbox Code Playgroud)
当我打电话时,showBottomSheet()我发现了这个异常:
java.lang.IllegalStateException:MonotonicFrameClock 在此 CoroutineContext 中不可用。调用者应使用 withContext 提供适当的 MonotonicFrameClock。在 androidx.compose.runtime.MonotonicFrameClockKt.getMonotonicFrameClock(MonotonicFrameClock.kt:114) 在 androidx.compose.runtime.MonotonicFrameClockKt.withFrameNanos(MonotonicFrameClock.kt:85) 在 androidx.compose.animation.core.SuspendAnimationKt.callWithFrameNanos(SuspendAnimation.kt :286)在androidx.compose.animation.core.SuspendAnimationKt.animate(SuspendAnimation.kt:229)在androidx.compose.animation.core.Animatable $ runAnimation $ 2.invokeSuspend(Animatable.kt:291)在androidx.compose.animation .core.Animatable$runAnimation$2.invoke(未知来源:8)在 androidx.compose.animation.core.Animatable$runAnimation$2.invoke(未知来源:2)在 androidx.compose.animation.core.MutatorMutex$mutate$2。 invokeSuspend(InternalMutatorMutex.kt:119) 在 androidx.compose.animation.core.MutatorMutex$mutate$2.invoke(未知来源:8) 在 androidx.compose.animation.core.MutatorMutex$mutate$2.invoke(未知来源:4)在 kotlinx.coroutines.intrinsics.UndispatchedKt.startUndispatchedOrReturn(Undispatched.kt:89)
我尝试使用其他协程范围,例如 Dispatcher.Main 等,但异常仍然相同。我该如何修复它?
小智 16
需要在Composition的范围内调用。
请检查此链接:https ://developer.android.com/jetpack/compose/side-effects#remembercoroutinescope
| 归档时间: |
|
| 查看次数: |
3879 次 |
| 最近记录: |