Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

760
Views
Types may only contain one @Inject constructor

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)

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!