Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How inject Map<> or Set<> (dagger 2 multibindings) with Toothpick

Dagger 2 has the ability to injecting set or map with multibindings. How to do it in Toothpick

for example i want @Inject constructor(val map: Map<String, ISyncRepository>)

like image 947
Jacob Avatar asked Nov 16 '25 04:11

Jacob


1 Answers

it might be like:

   @Binds
   @IntoMap
   @TheMapKey(“your key”)
   abstract fun getSyncRepository() : ISyncRepository

Look at the following 2 samples

https://google.github.io/dagger/multibindings.html

https://blog.kotlin-academy.com/understanding-dagger-2-multibindings-viewmodel-8418eb372848

for

@Inject constructor(

     private val creators: Map<Class<out ViewModel>, @JvmSuppressWildcards 
         Provider<ViewModel>>

) 

using:

@Binds
@IntoMap
@ViewModelKey(UserViewModel.class)
abstract ViewModel bindUserViewModel(UserViewModel userViewModel);
like image 71
lannyf Avatar answered Nov 18 '25 20:11

lannyf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!