How to inject ViewModel in the screen?
See original GitHub issueHi
I was trying to use the library with the ViewModel with the screen. I usually inject the ViewModel
into the screen via arguments as shown below.
SampleScreen
@Composable
@Destination(start = true)
fun SampleScreen(
viewModel: SampleViewModel,
navigator: DestinationsNavigator,
) {
// components
}
When I try the same, I am getting a runtime error.
2022-01-14 03:37:55.843 6495-6495/com.example.project E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.project, PID: 6495
java.lang.RuntimeException: SampleViewModel was requested, but it is not present
at com.example.project.destinations.SampleScreenDestination.Content(SampleScreenDestination.kt:34)
at com.ramcosta.composedestinations.DefaultNavHostEngine.CallComposable(DefaultNavHostEngine.kt:144)
at com.ramcosta.composedestinations.DefaultNavHostEngine.access$CallComposable(DefaultNavHostEngine.kt:30)
at com.ramcosta.composedestinations.DefaultNavHostEngine$addComposable$1.invoke(DefaultNavHostEngine.kt:104)
at com.ramcosta.composedestinations.DefaultNavHostEngine$addComposable$1.invoke(DefaultNavHostEngine.kt:103)
Could you please tell me if I missing some steps while setting up the library?
Navigation setup
@Composable
fun NavigationComponent(
modifier: Modifier = Modifier
) {
DestinationsNavHost(
navGraph = NavGraphs.root,
modifier = modifier
)
}
How would you suggest injecting the VMs? How would you use the library with the Hilt library?
Issue Analytics
- State:
- Created 2 years ago
- Reactions:3
- Comments:7 (4 by maintainers)
Top Results From Across the Web
How To Inject ViewModel With Dagger & What Might Go Wrong
The easiest way is to inject view model providers into a factory and map them manually: Add this factory to the component (AppComponent...
Read more >How to inject a ViewModel into a composable function using ...
From version androidx.hilt:hilt-navigation-compose:1.0.0-alpha02 you can inject view model into Composable functions by:
Read more >Injecting Profile ViewModel (Dagger 2 ViewModel Injection)
In this video I'm going to set up the ViewModel for ProfileFragment and inject like we did before with the ViewModel in AuthActivity....
Read more >Use Hilt with other Jetpack libraries - Android Developers
Inject ViewModel objects with Hilt ... Provide a ViewModel by annotating it with @HiltViewModel and using the @Inject annotation in the ViewModel ......
Read more >3 Ways to Tackle Assisted Injection for ViewModels
As for creating the ViewModel , you'll need to inject the factory and give it the Activity 's bundle to create the SavedStateHandle...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I’ll close the issue then, feel free to reopen it or add more comments if you want to 😃
Cool, I’ll create a PR with the suggestions for the documentation.
A really good solution, thanks. I’ll update the same in my project.