sk eat
06/17/2024, 6:46 AMBaseViewModel
and then have AAViewModel
and ABViewModel
inherit from it. We are using Hilt in our project to inject dependencies into the BaseViewModel
.
Here is an example of what I am trying to achieve:
kotlin
@HiltViewModel
open class BaseViewModel @Inject constructor(
private val sampleUseCase: SampleUseCase,
private val anotherUseCase: AnotherUseCase
) : ViewModel() {
// BaseViewModel logic
}
The problem arises when I try to implement the class that inherits from `BaseViewModel`:
kotlin
@HiltViewModel
class AAViewModel @Inject constructor(
private val secondSampleUseCase: SecondSampleUseCase
) : BaseViewModel(
// error! Too many parameters to pass
) {
// AAViewModel logic
}
When inheriting from BaseViewModel
, I need to pass many member variables through the constructor, which is cumbersome and error-prone. I want to eliminate the constructor parameters. How can I achieve this using Hilt?Rok Oblak
06/17/2024, 6:51 AMChrimaeon
06/17/2024, 7:08 AMwil
06/18/2024, 12:40 AMsk eat
06/18/2024, 4:50 AMPedro Varela
06/19/2024, 3:50 PM