Passing Application object to class in a compose preview function

215 Views Asked by At

I have the following preview function:

@Preview(
    name = "Light Mode",
)
@Composable
fun LM_HomePageSecondaryBarPreview() {
    val viewModel = HomePageViewModel(
        projectRepository = ProjectRepository(LocalContext.current.applicationContext as Application)
    )

    ProjectCounterTheme {
        Surface {
            HomePageSecondaryBar(
                viewModel = viewModel
            )
        }
    }
}

As you can see I need to provide ProjectRepository with an Application object. ChatGPT and it told me to do the above and I get the following error when it tries to build the preview:

java.lang.ClassCastException: class com.android.layoutlib.bridge.android.ApplicationContext cannot be cast to class android.app.Application

My question here is what do I need to do in order to pass Application to my ProjectRepository to be able to get the preview to work properly.

0

There are 0 best solutions below