question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to inject ViewModel in the screen?

See original GitHub issue

Hi

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:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
raamcostacommented, Jan 13, 2022

I’ll close the issue then, feel free to reopen it or add more comments if you want to 😃

1reaction
5AbhishekSaxenacommented, Jan 13, 2022

Cool, I’ll create a PR with the suggestions for the documentation.

Btw if people are not using Hilt, your solution might not be enough if you plan to access the navigation arguments from the SavedStateHandle.

A really good solution, thanks. I’ll update the same in my project.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found