我已经花了几个小时尝试在谷歌中查找我的两个 Dagger 实现的区别是什么。 是这样实现的
@Module
class MatchesModule
{
@Provides
@NetworkScope
@IntoMap
@RetrofitModulesName(eRetrofitModules.MATCHES)
fun retrofitMatches(okHttpClient: OkHttpClient, rxAdaptor: RxJava2CallAdapterFactory, iBuilder: Retrofit.Builder): Retrofit = iBuilder.addConverterFactory(GsonConverterFactory.create(mDeserializerMatches));
}
这个方法提供了Retrofit对象,我也使用注释@IntoMap
和@RetrofitModulesName(...)
为了把这一切Retrofit
要映射的对象。
@Module
class PreviewModule
{
@Provides
@PreviewScope
fun provideMatchesPresenter(retrofitModules: Map<eRetrofitModules, Retrofit>): IMatchPresenter = MatchPresenter(retrofitModules)
}
我正在获取所有Retrofit
对象并将它们传递给 MathcPresenter
一切都很好。
但我想得到 Map<Foo, Provider<Retrovit>>
在我的主持人中。
所以,我加了这个词Provider
论据
@Provides
@PreviewScope
fun provideMatchesPresenter(retrofitModules: Map<eRetrofitModules,
Provider<Retrofit>>): IMatchPresenter = MatchPresenter(retrofitModules)
以及 MathcPresenter
的构造函数
class MatchPresenter(retrofitModules: Map<eRetrofitModules, Provider<Retrofit>>): IMatchPresenter
现在我不明白为什么,但我得到这样的错误
<小时/>Error:(6, 1) error: [com.example.alexeyt.sunshinekotlin.moduls.previewmodule.PreviewComponent.inject(com.example.alexeyt.sunshinekotlin.ui.fragments.previewFragments.PreviewFragment)] java.util.Map> cannot be provided without an @Provides-annotated method.
预览范围
@Scope
@Retention(AnnotationRetention.RUNTIME)
annotation class PreviewScope
我做错了什么?
最佳答案
这可能是 Kotlin 处理泛型通配符的方式存在问题。
使用 Dagger 2 Multibinds 时,Dagger(它使用 Java Reflection 来分析代码并生成组件实现)将 Map 的类型解释为 Map<eRetrofitModules, ? extends Provider<Retrofit>>
。发生这种情况是因为 maps in Kotlin有他们的V
类型参数标记为 out
.
@JvmSuppressWildcards注释从编译的代码中删除该信息。只需在 Provider<Retrofit>
上使用该注释即可:
Map<eRetrofitModules, @JvmSuppressWildcards Provider<Retrofit>>
.
您还可以找到this answer有趣。
关于java - 为什么我收到 @Provides-annotated 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47991436/