https://kotlinlang.org logo
#dagger
Title
# dagger
m

Miguel

12/22/2021, 10:10 AM
Hello everyone, I got an issue with injecting something into a data class:
Copy code
data class Example @Inject constructor(
    val x: X,
    val string: String? = x.method()
)
In my dagger class I have:
Copy code
@Provides
@Singleton
fun x() = X()
but whenever I want to instantiate an
Example
object. it asks me for
x
dependency which should always be injected. Is there any approach of how I can deal with this?
d

Daniel Perez

12/22/2021, 2:54 PM
Since you’re generating two constructors with the default parameter on your
string
variable, try adding @JvmOverloads to the constructor right after @Inject so Dagger can see both constructors. If that doesn’t work, I would move your
string
variable as a member variable instead of a constructor parameter.
4 Views