Ad

Inject Only Certain Params In Constructor

- 1 answer

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?

Ad

Answer

Make inject constructor with secondary constructor

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