[英]FirebaseFirestore cannot be provided without an @Inject constructor or an @Provides-annotated method
Im struggling with the following issue and I cant find a solution.我正在努力解决以下问题,但找不到解决方案。 I have multi module project.
我有多模块项目。 I set up all the modules and dependencies but im still getting this error for firestore: error: [Dagger/MissingBinding] com.google.firebase.firestore
我设置了所有模块和依赖项,但我仍然收到 firestore 的此错误:错误:[Dagger/MissingBinding] com.google.firebase.firestore
My DI setup is following我的 DI 设置如下
FirebaseDiModule(part of the firestore module) `@Module @InstallIn(SingletonComponent::class) object FirebaseDiModule { FirebaseDiModule(firestore 模块的一部分)`@Module @InstallIn(SingletonComponent::class) object FirebaseDiModule {
@Singleton
@Provides
fun provideFirebaseAuth(): FirebaseAuth {
return FirebaseAuth.getInstance()
}
@Singleton
@Provides
fun provideFirestoreFirebase(): FirebaseFirestore {
return FirebaseFirestore.getInstance()
}
@Singleton
@Provides
fun provideUserFirebaseDataSource(
firebaseFirestore: FirebaseFirestore,
firebaseAuth: FirebaseAuth,
): UserFirestoreDataSource {
return UserFirestoreDataSource(
firebaseFirestore = firebaseFirestore,
firebaseAuth = firebaseAuth,
)
}
}` }`
UserFirestoreDataSource (part of the firebase module) UserFirestoreDataSource(firebase 模块的一部分)
class UserFirestoreDataSource @Inject constructor( private val firebaseFirestore: FirebaseFirestore, private val firebaseAuth: FirebaseAuth, )
Then I have authentication module which is beasicly a feature module contaning jetpack compose and viewmodel's.然后我有身份验证模块,它基本上是一个包含 jetpack compose 和 viewmodel 的功能模块。
The ViewModel i use is set as this:我使用的 ViewModel 设置如下:
@HiltViewModel class OnBoardingViewModel @Inject constructor( userFirestoreDataSource: UserFirestoreDataSource, ): ViewModel() {
The authentication module is added to the app module where I use the OnBoardingViewModel and composables.身份验证模块已添加到我使用 OnBoardingViewModel 和可组合项的应用程序模块。
In the app module I have this:在应用程序模块中,我有这个:
@HiltAndroidApp class AppController: Application()
@AndroidEntryPoint class MainActivity: ComponentActivity()
When I run the project I get the following error:当我运行该项目时,出现以下错误:
error: [Dagger/MissingBinding] com.google.firebase.firestore.FirebaseFirestore cannot be provided without an @Inject constructor or an @Provides-annotated method.错误:[Dagger/MissingBinding] com.google.firebase.firestore.FirebaseFirestore 无法在没有@Inject 构造函数或@Provides 注释方法的情况下提供。 public abstract static class SingletonC implements AppController_GeneratedInjector,
公共摘要 static class SingletonC 实现 AppController_GeneratedInjector,
But this error only occurs when I add @HiltViewModel annotation to the OnBoardingViewModel.但是只有当我向 OnBoardingViewModel 添加 @HiltViewModel 注释时才会出现此错误。 Im really frustrated by this problem and I cant figure it out what am I doing wrong.
我真的对这个问题感到沮丧,我无法弄清楚我做错了什么。
There is nothing to try because Its a DI setup没有什么可尝试的,因为它是一个 DI 设置
You need to create a @HiltViewModel class and annotate it with the @HiltViewModel
annotation.您需要创建一个 @HiltViewModel class 并使用
@HiltViewModel
注释对其进行注释。
here is an example code:这是一个示例代码:
@HiltViewModel
class OnBoardingViewModel @ViewModelInject constructor(
userFirestoreDataSource: UserFirestoreDataSource
) : ViewModel() {
//ViewModel code
}
it's like telling DI (@ViewModelInject) hey this is a ViewModel class inject the necessary dependencies into the ViewModel这就像告诉 DI (@ViewModelInject) 嘿这是一个 ViewModel class 将必要的依赖项注入 ViewModel
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.