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:
The setup is a simple multi-module Android App with Hilt/Dagger.
Hello,
I'm trying out Voyager which seems amazing but I'm stuck on using an Android ViewModel within a Screen. The screen:
A ViewModel with an empty constructor works:
But as soon as I add a property to inject, it doesn't:
The error is the following:
Calling the same ViewModel from the only Activity works:
ScreenModel does work with an injected property:
Some more info/comments:
AndroidScreen()
which could help but I can't find it, was that removed? What replaces it?So the question is, why is it still looking for an empty constructor? What am I missing? Thanks in advance! :pray: