21

Data Model

data class AuthDataModel @Inject constructor(
                   var username: String = "",
                   var password: String = "",
                   var mobileData: String = "

Explanation

I am trying to inject authentication data model to authentication view model in kotlin, but it does not compile with message("Types may only contain one @Inject constructor)

Hrant Nurijanyan
  • 789
  • 2
  • 9
  • 26
  • 2
    IIRC `= ""` in the primary constructor generates several constructors pointing to that. Decompile the file to verify though. If it does, you should have 4 constructors (empty, username, username and password, username, password, and mobile data). – Zoe Mar 19 '19 at 11:55
  • 2
    @Zoe Thanks. I removed default value for properties and it worked. – Hrant Nurijanyan Mar 19 '19 at 11:58

1 Answers1

40

Moving my comment to an answer:

If you have a constructor with default arguments, Kotlin actually generates additional constructors. In your case, you have a 3 arg constructor where all are optional, which generates a total of 4 constructors. Kotlin apparently associates any annotations on the primary constructor with all the generated ones as well, which means you ended up with 4 @Inject constructors.

You have two options:

The first, as you mentioned yourself, remove all the default values. If there are no default values, only one constructor is generated with the annotation.

Alternatively, you can also create additional constructors yourself and point it to the primary. This would also let you manually specify only one to have the @Inject annotation, while the others don't. Basically:

data class AuthDataModel @Inject constructor(
                   var username: String,
                   var password: String,
                   var mobileData: String) {
    constructor(username: String) : this(username, "", "") {}
    constructor(username: String, password: String) : this(username, password, "") {}
}

Not using default values prevents multiple @Inject constructors from being generated, and the secondary constructors should1 keep everything functioning as expected. This is basically overloading the constructor, and it's equivalent to what you'd do in Java when certain variables are optional. Should therefore be fine.

1: I haven't done Android in a while, and I've never used @Inject. If option 2 doesn't work (as in @Inject doesn't allow it, or doesn't work as expected, etc.), that only leaves option 1, and requires every parameter to be explicitly passed. The secondary constructors calling the primary constructor should be enough to keep everything working, though.

Zoe
  • 27,060
  • 21
  • 118
  • 148
  • Thanks! Also don't forget to change all created objects of this class (because parameter list has changed). – CoolMind Sep 20 '21 at 09:46