2017-12-27 193 views
0

我已经花了数小时尝试在谷歌中找到我的两个匕首实现的区别。 它实现这样为什么我得到@提供注释错误?

@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(...)

@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 

的构造函数,现在我不能undersand为什么,但我得到这样的错误

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.


PreviewScope

@Scope 
@Retention(AnnotationRetention.RUNTIME) 
annotation class PreviewScope 

我在做什么错?

+0

什么是'@ PreviewScope'? –

+0

@DivyeshKalotra加入问题 –

+1

@DivyeshKalotra但你为什么要问'预览范围'?据我了解这是没有关系......它是完美的作品,直到我加入提供程序<>'的参数 –

回答

2

这可能是Kotlin处理泛型通配符的一个问题。

当使用Dagger 2 Multibindinds时,Dagger(使用Java Reflection分析代码并生成组件实现)将映射类型解释为Map<eRetrofitModules, ? extends Provider<Retrofit>>。发生这种情况是因为maps in KotlinV类型参数标记为out

@JvmSuppressWildcards注解从编译后的代码中删除该信息。只需在Provider<Retrofit>上使用该注释:

Map<eRetrofitModules, @JvmSuppressWildcards Provider<Retrofit>>

您还可能会发现this answer有趣。

+0

是的,它帮助。但我之前找到了这个解决方案,并试图......但没有......也许在另一个地方。好的,无论如何非常感谢 –