hiltViewModel & @HiltViewModel
Inject ViewModels into Composables.
ViewModels Need Dependencies Too
Your ViewModel usually depends on a repository or use case. Hilt can inject those for you, so you never build them by hand again.
Annotate the ViewModel
Mark the class with @HiltViewModel and add @Inject to its constructor. Hilt now knows how to create it with all its dependencies.
@HiltViewModel
class FeedViewModel @Inject constructor(private val repo: Repo) : ViewModel()All lessons in this course
- Why DI: Testable, Decoupled Code
- Modules, @Provides & @Binds
- hiltViewModel & @HiltViewModel
- Scopes & Component Lifetimes