androidkotlindagger-2dagger

Inject only certain params in constructor


I have the presenter

class PhonePresenter @Inject constructor(
    private val preference: DataPreference,
    private val ioScheduler: Scheduler = Schedulers.io())

ioScheduler is a default parameter. I want to inject only preference: DataPreference. Now I have the exception

[dagger.android.AndroidInjector.inject(T)] io.reactivex.Scheduler cannot be provided without an @Provides-annotated method.

Is there any way to define parameters which I want to inject in a constructor?


Solution

  • Make inject constructor with secondary constructor

    class PhonePresenter(
        private val preference: DataPreference,
        private val ioScheduler: Scheduler) {        
        @Inject constructor(preference: DataPreference) : this(preference, Schedulers.io())
    }