How do you inject UserRepository into a ViewModel with Hilt?

Tests Hilt constructor injection for classes you own. Annotate UserRepository's constructor with @Inject so Hilt auto-provides it to consumers like a @HiltViewModel. Red flag: using a @Provides module for a class you control.
This tests your grasp of Hilt's compile-time dependency graph and constructor injection for classes you own. A strong answer explains that you annotate UserRepository's constructor with @Inject, which lets Hilt automatically provide it anywhere it is requested, including inside a @HiltViewModel. You should note that ApiService must also be injectable, either via its own @Inject constructor or a module.
Read the original → developer.android.com
- #android
- #hilt
- #dependency-injection
- #kotlin
- #viewmodel
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.