Open RowlandOti opened 5 months ago
To fix this, I had to use:
@Composable
fun Navigation(navController: NavHostController) {
NavHost(navController, startDestination = Screen.Main.route) {
navigation(startDestination = Screen.Home.route, route = Screen.Main.route) {
composable(Screen.Home.route) {
val createReceiptViewModel: CreateReceiptViewModel = hiltViewModel<CreateReceiptViewModel>()
}
}
}
instead of :
@Composable
fun Navigation(navController: NavHostController) {
val createReceiptViewModel: CreateReceiptViewModel = hiltViewModel<CreateReceiptViewModel>()
NavHost(navController, startDestination = Screen.Main.route) {
navigation(startDestination = Screen.Home.route, route = Screen.Main.route) {
composable(Screen.Home.route) {
}
}
}
Notice the difference in where ViewModel is initialized.
This CreationExtra is generally added by the owner, like here in ComponentActivity
. There's similar code in Fragments and other places.
This doesn't quite seem like a Hilt issue. If you try this with a non-Hilt ViewModel that uses a SavedStateHandle, do you get this error?
This doesn't quite seem like a Hilt issue. If you try this with a non-Hilt ViewModel that uses a SavedStateHandle, do you get this error?
@Chang-Eric unfortunately I only get this error when using KMP module and HIlt - unless I add the viewModel manually by by-passing Hilt. If I convert KMP to just normal Java/Android module, the error goes a away. (Same dependency versions between the two) . My project is a pure Jetpack compose.
Did you check that the owner adds that key in getDefaultViewModelCreationExtras()
as I linked for ComponentActivity? What's the owner of the ViewModel?
When running project that has HILT, I am getting the following crash caused by:
val createReceiptViewModel: CreateReceiptViewModel = hiltViewModel<CreateReceiptViewModel>(key = "create_bill_viewmodel")
on the stacktrace:
Dependencies:
This issue only happens when I try to consume a library that is compiled with KMP