Daniel Weidensdörfer
06/21/2021, 2:43 PMModalBottomSheetLayout
, that has a composable as sheetContent
that is basically a different screen and therefore has a different ViewModel, I get the following Error:
java.lang.IllegalStateException: ViewModels creation is not supported in Preview
A similar error occurs on testing.
Here is an example:
@ExperimentalMaterialApi
@Composable
fun MyScreen(screenViewModel: ScreenViewModel = viewModel()) {
val state = rememberModalBottomSheetState(ModalBottomSheetValue.Hidden)
ModalBottomSheetLayout(sheetState = state, sheetContent = {
Surface {
MyDialog()
}
}) {
// ScreenContent
}
}
@Composable
fun MyDialog(dialogViewModel: DialogViewModel = viewModel()) {
// Dialog content that needs dialogViewModel
}
Am I supposed to use screenViewModel
also for MyDialog
?
If yes, what is the correct way of reusing MyDialog in other screens if it cannot have its own ViewModel?
If no, how can I work around this error?