简体   繁体   English

如果没有 @Inject 构造函数或 @Provides-annotated 方法,则无法提供 FirebaseFirestore

[英]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.

相关问题 Dagger:不能在没有 @Inject 构造函数或 @Provides 注释的方法的情况下提供 - Dagger: cannot be provided without an @Inject constructor or an @Provides-annotated method okhttp3.Cache必须在没有@Inject构造函数或@Provides注释方法的情况下提供 - okhttp3.Cache cannot be provided without an @Inject constructor or from an @Provides-annotated method 如何修复 - 如果没有 @Inject 构造函数或 @Provides-annotated 方法,则无法提供 - How to fix - cannot be provided without an @Inject constructor or an @Provides-annotated method Dagger2:编译错误:没有@Inject构造函数或@Provides注释方法无法提供 - Dagger2: Compile error: cannot be provided without an @Inject constructor or from an @Provides-annotated method 如何解决 dagger 错误 - 如果没有 @Inject 构造函数或 @Provides-annotated 方法,则无法提供字符串 - How to resolve dagger error - String cannot be provided without an @Inject constructor or an @Provides-annotated method 没有@Inject 构造函数或@Provides-annotated 方法无法提供活动| 从 Dagger2 迁移到 Hilt - Activity cannot be provided without an @Inject constructor or an @Provides-annotated method | Migrating from Dagger2 to Hilt Dagger 2错误:依赖项“如果没有@Provides注释的方法就无法提供” - Dagger 2 error: dependency “cannot be provided without an @Provides-annotated method" 如果没有@ Provide-annotated方法,则无法提供Dagger 2 - Dagger 2 cannot be provided without an @Provides-annotated method Dagger 2:没有 @Provides 注释的方法就无法提供 - Dagger 2: Cannot be provided without an @Provides-annotated method 没有@Provides注释的方法就无法提供对象 - Objects cannot be provided without an @Provides-annotated method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM