I have the presenter

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

ioScheduler是默认参数.我只想注射preference: DataPreference支.现在我有个例外

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

有没有办法定义我想注入构造函数的参数?

推荐答案

使用辅助构造函数创建注入构造函数

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

Kotlin相关问答推荐

何时使用figureEach

如何在Docker中使用Selenium和chromedriver?

如何更改默认推断没有接收者的函数类型?

如何将消费者放入 Kotlin 的 map 中?

Kotlin 获取继承类的默认 hashCode 实现

Webpack 配置未应用于 kotlin 多平台react 项目

Kotlin 可空泛型

内容更改后的 var 重新计算

在 SplashActivity 中显示的 Firebase 应用内消息.如何在 MainActivity 中显示它?

未为任务启用 Gradle 构建缓存

SpringBoot 2.5.0 对于 Jackson Kotlin 类的支持,请在类路径中添加com.fasterxml.jackson.module: jackson-module-kotlin

如何退出 Kotlinc 命令行编译器

ActivityOptions.makeSceneTransitionAnimation 在具有多个视图的 kotlin 中不起作用

Android:在 DAO 中使用 Room 数据库和 LiveData 架构

Kotlin协程处理错误和实现

大小写敏感性 Kotlin / ignoreCase

类型不匹配:推断类型为 LoginActivity 但应为 LifecycleOwner

为什么 Kotlin 会收到这样的 UndeclaredThrowableException 而不是 ParseException?

Android room DAO 接口不适用于继承

如何在 spring-boot Web 客户端中发送请求正文?