I want to inject viewModelscope but I just couldn't do it.
class PostPageSource @Inject constructor(
val repository: MyRepository,
val scope: CoroutineScope,
val mapper: MyMapper
) : PageKeyedDataSource<Int, Posts>() {
The error output is as follows:
[Dagger/MissingBinding] kotlinx.coroutines.CoroutineScope cannot be provided without an @Provides-annotated method.
public abstract static class ApplicationC implements App_GeneratedInjector,
I tried to:
@Module
@InstallIn(FragmentComponent::class)
object PagingModule {
@Singleton
@Provides
fun provideViewModel(fragment: Fragment) : UserDetailViewModel {
val viewModel: UserDetailViewModel by (fragment as UserDetailFragment).viewModels()
return viewModel
}
fun provideCorountineScope(fragment: UserDetailViewModel): CoroutineScope {
return fragment.viewModelScope
}
}
The error output is as follows:
error: [Dagger/MissingBinding] kotlinx.coroutines.CoroutineScope cannot be provided without an @Provides-annotated method.
public abstract static class ApplicationC implements App_GeneratedInjector,
^
kotlinx.coroutines.CoroutineScope is injected at
com.maksu.insider.userdetail.paging.PostPageSource(…, scope, …)
javax.inject.Provider<com.maksu.insider.userdetail.paging.PostPageSource> is injected at
com.maksu.insider.userdetail.paging.PostPageSourceFactory(providerDataSource)
javax.inject.Provider<com.maksu.insider.userdetail.paging.PostPageSourceFactory> is injected at
com.maksu.insider.userdetail.UserDetailViewModel_AssistedFactory(…, dataSourceFactory)
com.maksu.insider.userdetail.UserDetailViewModel_AssistedFactory is injected at
com.maksu.insider.userdetail.UserDetailViewModel_HiltModule.bind(factory)
java.util.Map<java.lang.String,javax.inject.Provider<androidx.hilt.lifecycle.ViewModelAssistedFactory<? extends androidx.lifecycle.ViewModel>>> is injected at
androidx.hilt.lifecycle.ViewModelFactoryModules.ActivityModule.provideFactory(…, viewModelFactories)
@dagger.hilt.android.internal.lifecycle.DefaultActivityViewModelFactory java.util.Set<androidx.lifecycle.ViewModelProvider.Factory> is requested at
dagger.hilt.android.internal.lifecycle.DefaultViewModelFactories.ActivityEntryPoint.getActivityViewModelFactory() [com.maksu.insider.App_HiltComponents.ApplicationC → com.maksu.insider.App_HiltComponents.ActivityRetainedC → com.maksu.insider.App_HiltComponents.ActivityC]
The following other entry points also depend on it:
I don't know why you want to do that, but I think you miss some @Provides
at the top of your function:
@Provides
fun provideCorountineScope(fragment: UserDetailViewModel): CoroutineScope {
return fragment.viewModelScope
}
But I really don't recommend doing this. If you want a coroutine that should last as long as the viewmodel does, then just launch the coroutine inside the viewmodel.
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.