I am using Hilt and navigation so when inject view...
# compose
a
I am using Hilt and navigation so when inject viewmodel without parameters it works but when viewModel has other classes injected i has crash
Caused by: java.lang.InstantiationException: java.lang.Class<com.xxx.screen.section.SViewModel> has no zero argument constructor
n
Have you tried this:
Copy code
val viewModel: MainViewModel = hiltViewModel()
That's what worked for me.
a
ok i try thanks
so this
Copy code
val viewModel = viewModel<SViewModel>()
    SectionPage(viewModel, backStackEntry.arguments?.getString("type"))
can’t work
n
No, no, I mean when you are calling your viewModel in your Composable
a
i create viewModel in navifation
n
Ah ok, sorry
a
Copy code
composable(Destination.SectionScreenPage.path) { backStackEntry ->
    val viewModel = viewModel<SViewModel>()
        SectionPage(viewModel)
    
    
    }

}
n
Have you tried replacing
val viewModel = viewModel<SViewModel>()
with
val viewModel: SViewModel = hiltViewModel()
?
a
it works, great
n
cool