Reputation: 297
I am getting this error:
[Dagger/MissingBinding] SessionFetcher cannot be provided without an @Inject constructor or an @Provides-annotated method. while you can see I have defined SessionFetcher
in AppModule class
@Suppress("unused")
@Module
abstract class ViewModelModule {
@Binds
@IntoMap
@ViewModelKey(AccountViewModel::class)
abstract fun bindAccountViewModel(accountViewModel: AccountViewModel): ViewModel
@Binds
abstract fun bindViewModelFactory(factory: ViewModelProviderFactory): ViewModelProvider.Factory
}
@Module(includes = [ViewModelModule::class])
class AppModule(private val applicationContext: Context) {
@Provides
fun sessionFetcher(@Named("UserPreferences") userPreferences: SharedPreferences): ISessionFetcher {
return SessionFetcher(userPreferences)
}
@Singleton
@Provides
@Named("UserPreferences")
fun userPreferences(context: Context): SharedPreferences {
return context.getSharedPreferences(USER_CONFIG_NAME, Context.MODE_PRIVATE)
}
class AccountViewModel @Inject constructor(sessionFetcher: SessionFetcher) :
SessionViewModel(sessionFetcher), AppComponent.Injectable {
Upvotes: 1
Views: 110
Reputation: 31458
You're providing ISessionFetcher
inside your graph, but you're trying to inject
an instance of SessionFetcher
.
What you're providing to the graph is the type you can inject. Dagger does not analyze bodies of @Providing
functions (or types for the eventual implementing class).
Just inject the interface instead of the implementing class.
class AccountViewModel @Inject constructor(sessionFetcher: ISessionFetcher) :
SessionViewModel(sessionFetcher), AppComponent.Injectable {
//...
}
if you need to use methods of SessionFetcher
that are not exposed by the ISessionFetcher
, then your ISessionFetcher
is probably incomplete.
Upvotes: 1