Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To use Hilt for injecting a list into a ViewModel, you can follow the below steps:

  1. Create a module that provides the list of objects that you want to inject:
@Module
@InstallIn(SingletonComponent::class)
object MyModule {

    @Provides
    fun provideMyList(): List<MyObject> {
        // return your list of objects
    }
}
  1. Add this module to your Hilt application component:
@HiltAndroidApp
class MyApp : Application()
  1. Add the @Inject annotation to your ViewModel constructor:
class MyViewModel @Inject constructor(private val myList: List<MyObject>) : ViewModel() {

    // use the injected list in your ViewModel logic
}

Hilt will automatically instantiate your ViewModel and inject the list of objects into its constructor.