Issue
I am trying two separate path collection query but hilt is throwing up a error of:
[Dagger/DuplicateBindings] com.google.firebase.firestore.CollectionReference is bound multiple times
How should i use two Collection Reference from my Repository to my AppModule?
@Module
@InstallIn(SingletonComponent::class)
object AppModule {
@Provides
fun provideFirebaseAuth(): FirebaseAuth = Firebase.auth
@Provides
fun provideFirebaseFirestore() = Firebase.firestore
@Provides
@Singleton
fun provideProductRef(db: FirebaseFirestore) = db.collection(Products)
@Provides
@Singleton
fun provideCategoriesRef(catDb: FirebaseFirestore) = catDb.collection(Categories)
@Provides
@Singleton
fun provideProductRepository(
productRef: CollectionReference,
cartRef: CollectionReference,
catRef: CollectionReference
): ProductRepository = ProductRepositoryImpl(productRef, cartRef,catRef)
@Provides
@Singleton
fun provideUseCases(productRepo: ProductRepository) = UseCases(
getProducts = GetProducts(productRepo),
addToCart = AddToCart(productRepo),
getCategories = GetCategories(productRepo)
)
My Repository
typealias Products = List<Product>
typealias Categories = List<Category>
typealias ProductResponse = Response<Products>
typealias CategoryResponse = Response<Categories>
typealias AddProductToCartResponse = Response<Boolean>
interface ProductRepository {
fun getProductsFromFirestore(): Flow<ProductResponse>
fun getCategoriesFromFirestore(): Flow<CategoryResponse>
suspend fun addProductToCartCollection(product: Product): AddProductToCartResponse
}
Error
/home/shiva/AndroidStudioProjects/ZuZu/app/build/generated/hilt/component_sources/debug/com/shivaconsulting/zuzu/ZuzuApp_HiltComponents.java:131: error: [Dagger/DuplicateBindings] com.google.firebase.firestore.CollectionReference is bound multiple times: It is also requested at:
com.shivaconsulting.zuzu.di.AppModule.provideProductRepository(productRef, …)
com.shivaconsulting.zuzu.di.AppModule.provideProductRepository(…, catRef)
Solution
You are getting the following error:
[Dagger/DuplicateBindings] com.google.firebase.firestore.CollectionReference is bound multiple times
Because of the presence of the following lines of code inside your AppModule class:
@Provides
@Singleton
fun provideProductRef(db: FirebaseFirestore) = db.collection(Products)
@Provides
@Singleton
fun provideCategoriesRef(catDb: FirebaseFirestore) = catDb.collection(Categories)
This means that you trying to create two objects of type CollectionReference, which is actually not possible because Hilt won't know which one to inject. To solve this, you have to differentiate them by naming them differently:
@Provides
@Singleton
@Named("products") //👈
fun provideProductsRef(db: FirebaseFirestore) = db.collection("products")
@Provides
@Singleton
@Named("categories") //👈
fun provideCategoriesRef(db: FirebaseFirestore) = db.collection("categories")
@Provides
@Singleton
@Named("cart") //👈
fun provideCartRef(db: FirebaseFirestore) = db.collection("cart")
Going forward, when you need to create a repository object you have to explicitly specify the names:
@Provides
@Singleton
fun provideProductRepository(
@Named("products") //👈
productsRef: CollectionReference,
@Named("categories") //👈
categoriesRef: CollectionReference,
@Named("cart") //👈
cartRef: CollectionReference
): ProductRepository = ProductRepositoryImpl(productsRef, categoriesRef, cartRef)
Later, when you want to use these three objects inside your repository implementation class you have to use:
@Singleton
class ProductRepositoryImpl @Inject constructor(
@Named("products") //👈
private val productsRef: CollectionReference,
@Named("categories") //👈
private val categoriesRef: CollectionReference,
@Named("cart") //👈
private val cartRef: CollectionReference
): ProductRepository {
//...
}
Answered By - Alex Mamo
Answer Checked By - Willingham (JavaFixing Volunteer)