Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Types may only contain one @Inject constructor

Tags:

android

kotlin

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)

like image 999
Hrant Nurijanyan Avatar asked Mar 19 '19 11:03

Hrant Nurijanyan


People also ask

What is @inject in constructor?

4.1. The @Inject annotation lets us define an injection point that is injected during bean instantiation. Injection can occur via three different mechanisms. Bean constructor parameter injection: public class Checkout { private final ShoppingCart cart; @Inject.

How do you inject a constructor in Kotlin?

We use @Inject annotation at the top of the constructor. Field Injection: In this technique , the dependencies are injected as part of fields in the classes that require them. @Inject annotation is used for the fields to be injected.


1 Answers

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.

like image 85
Zoe stands with Ukraine Avatar answered Oct 20 '22 23:10

Zoe stands with Ukraine