Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Dagger-Hilt] Can't get viewModel in composable by Hilt #4279

Open
Anatoly4444 opened this issue Apr 4, 2024 · 1 comment
Open

[Dagger-Hilt] Can't get viewModel in composable by Hilt #4279

Anatoly4444 opened this issue Apr 4, 2024 · 1 comment

Comments

@Anatoly4444
Copy link

Hilt can't provide View Model by factory using viewModel() function in composable function. Every time I got:

Caused by: java.lang.NoSuchMethodException: com.example.myapplication.HomeViewModel.<init>

My ViewModel class
@HiltViewModel class HomeViewModel @Inject constructor(val testClass: TestClass) : ViewModel() { fun test() { testClass.test() } }

Composable:
`@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
@composable
fun HomeScreen(
modifier: Modifier = Modifier,
viewModel: HomeViewModel = viewModel()
) {
Button(onClick = { /TODO/ }) {

}
}`

@danysantiago
Copy link
Member

This should work and is mentioned in the Compose + Hilt docs: https://developer.android.com/develop/ui/compose/libraries#hilt

Please double check your setup, that the Activity or Fragment are annotated with @AndroidEntryPoint. That your runtime and annotation processor dependencies are in the module and that the Hilt Gradle plugin is applied.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants