简体   繁体   中英

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

My DI setup is following

FirebaseDiModule(part of the firestore module) `@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)

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.

The ViewModel i use is set as this:

@HiltViewModel class OnBoardingViewModel @Inject constructor( userFirestoreDataSource: UserFirestoreDataSource, ): ViewModel() {

The authentication module is added to the app module where I use the OnBoardingViewModel and composables.

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. public abstract static class SingletonC implements AppController_GeneratedInjector,

But this error only occurs when I add @HiltViewModel annotation to the OnBoardingViewModel. 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

You need to create a @HiltViewModel class and annotate it with the @HiltViewModel annotation.

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

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