简体   繁体   中英

Error dagger missingBinding in Kotlin with dagger 2

I'm facing a little issue after i tried to implement Dagger 2 in Koltin, i almost tried everything i saw in other topic on Stack over flow and other articles but nothing worked out for me, this is the error i'm facing, if any one can kindly help or guide me to solve it, thank you

  • This is the error generated
error: [Dagger/MissingBinding] java.util.Map<java.lang.Class<? extends androidx.lifecycle.ViewModel>,? extends javax.inject.Provider<androidx.lifecycle.ViewModel>> cannot be provided without an @Provides-annotated method.
public abstract interface MainComponent extends dagger.android.AndroidInjector<com.example.foodapp.di.BaseApplication> {
                ^
      java.util.Map<java.lang.Class<? extends androidx.lifecycle.ViewModel>,? extends javax.inject.Provider<androidx.lifecycle.ViewModel>> is injected at
          com.example.foodapp.di.DiComponents.ViewModelProviderFactory(creators)
      com.example.foodapp.di.DiComponents.ViewModelProviderFactory is injected at
          com.example.foodapp.MainActivity.provider
      com.example.foodapp.MainActivity is injected at

  • This is my AuthViewModelModule where i map my viewmodel class
@Module
abstract class AuthViewModelsModule {
    @Binds
    @IntoMap
    @ViewModelKey(MainViewModel::class)
    abstract fun bindAuthViewModel(viewModel: MainViewModel): ViewModel?
}
  • This is my ViewModelKey
@MustBeDocumented
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
@MapKey
internal annotation class ViewModelKey(val value: KClass<out ViewModel>)
  • This is my ViewModelProvierFactory
lass ViewModelProviderFactory @Inject constructor(private val creators: Map<Class<out ViewModel>, Provider<ViewModel>>) :
    ViewModelProvider.Factory {
    override fun <T : ViewModel?> create(modelClass: Class<T>): T {
        var creator: Provider<out ViewModel>? = creators[modelClass]
        if (creator == null) { // if the viewmodel has not been created

            // loop through the allowable keys (aka allowed classes with the @ViewModelKey)
            for ((key, value) in creators) {

                // if it's allowed, set the Provider<ViewModel>
                if (modelClass.isAssignableFrom(key)) {
                    creator = value
                    break
                }
            }
        }

        // if this is not one of the allowed keys, throw exception
        requireNotNull(creator) { "unknown model class $modelClass" }

        // return the Provider
        return try {
            creator.get() as T
        } catch (e: Exception) {
            throw RuntimeException(e)
        }
    }

    companion object {
        private const val TAG = "ViewModelProviderFactor"
    }

}

  • This is AppComponent class
@Component(modules = [AndroidSupportInjectionModule::class,AndroidBuildersModule::class, ViewModelFactoryModule::class, AppModule::class])
interface MainComponent : AndroidInjector<BaseApplication>{

    @Component.Builder
    interface toBuild
    {
        @BindsInstance
        fun Building(application: Application) : toBuild
        fun build() : MainComponent
    }
}

It looks like you forgot to add AuthViewModelsModule.class to modules in the AppComponent class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM