adrielcafe / voyager

🛸 A pragmatic navigation library for Jetpack Compose
https://voyager.adriel.cafe
MIT License
2.31k stars 109 forks source link

ViewModel and injected property issue with Hilt #311

Open galex opened 4 months ago

galex commented 4 months ago

Hello,

I'm trying out Voyager which seems amazing but I'm stuck on using an Android ViewModel within a Screen. The screen:

class SignupScreen: Screen {

    @Composable
    override fun Content() {
        val screenModel: SignupScreenModel = getScreenModel()
        val viewModel: SignupViewModel = getViewModel()
        Text("Signup Screen right here!")
    }
}

A ViewModel with an empty constructor works:

@HiltViewModel
class SignupViewModel @Inject constructor(): ViewModel() 

But as soon as I add a property to inject, it doesn't:

@HiltViewModel
class SignupViewModel @Inject constructor(
    private val signupUseCase: SignupUseCase,
): ViewModel()

The error is the following:

FATAL EXCEPTION: main
Process:  com.company, PID: 30741
    java.lang.RuntimeException: Cannot create an instance of class com.company.feature.SignupViewModel
(...)
Caused by: java.lang.NoSuchMethodException: com.company.feature.SignupViewModel.<init> []
    at java.lang.Class.getConstructor0(Class.java:3325)
    at java.lang.Class.getDeclaredConstructor(Class.java:3063)
    at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.kt:199)

Calling the same ViewModel from the only Activity works:

val viewModel: SignupViewModel by viewModels()

ScreenModel does work with an injected property:

class SignupScreenModel @Inject constructor(
    private val signupUseCase: SignupUseCase,
): ScreenModel

Some more info/comments:

So the question is, why is it still looking for an empty constructor? What am I missing? Thanks in advance! :pray:

Anatoly4444 commented 1 month ago

I facing the same issue. I don't have empty constructor in my ViewModel class but factory trying to call it