I have a simple class as below
class MainString(val msg: String)
我想给它注入不同的参数,所以我按照https://google.github.io/dagger/users-guide中所示的指南使用@Named
限定符
With that my AppModule has
@Provides @Named("Two")
fun provideTwoMainString(): MainString {
return MainString("Two")
}
@Provides @Named("One")
fun provideOneMainString(): MainString {
return MainString("One")
}
在我的MainActivity中,我只需呼叫
@Inject @Named("One")
lateinit var stringOne: MainString
@Inject @Named("Two")
lateinit var stringTwo: MainString
然而,当我编译时,它会抱怨
Error:(11, 1) error: com.elyeproj.demo_dagger_scope.MainString cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method.
It seems to want me to provide another Provider without the qualifier. So if I add the below, all will compiles. But it is not of used to me, as I want to have different argument injection.
@Provides
fun provideMainString(): MainString {
return MainString("Solo")
}
What have I done wrong?