Search code examples
androidkotlinretrofitdata-class

Initialise data class object to 1 or 2 arguements in kotlin?


I have the below data class

data class ApiPost(
@SerializedName("LoginId")  var userName: String,
@SerializedName("Password") var password: String,
@SerializedName("NewPassword") var newPassword: String,
@SerializedName("FileType") var FileType: String,
@SerializedName("UserId") var UserId: String,
@SerializedName("CountryId") var CountryId: String,
@SerializedName("DateOfBirth") var DateOfBirth: String,
@SerializedName("Mobile") var Mobile: String,
@SerializedName("CountryName") var CountryName: String,
@SerializedName("CompanyName") var CompanyName: String,
@SerializedName("IsAnonymous") var IsAnonymous: String,
@SerializedName("EmployeeIssue") var EmployeeIssue: String,
@SerializedName("DetailedInformation") var DetailedInformation: String,
@SerializedName("EmployeeId") var EmployeeId: String,
@SerializedName("EmployeeEmailId") var EmployeeEmailId: String,
@SerializedName("FCMId") var FCMId: String
)

To initialise the object I will have to pass all the values.

But my question is what if I only want to pass 2 values to object. How can that be done?

val apiPost: ApiPost=ApiPost()
                    apiPost!!.userName = "ak@gmail.com"
                    apiPost!!.password = "12345"

What needs to be done here?


Solution

  • Thanks to the answer over here Kotlin data class optional variable

    Initialised default value to the data class

    data class ApiPost(
            @SerializedName("LoginId")  var userName: String ="",
            @SerializedName("Password") var password: String ="",
            @SerializedName("NewPassword") var newPassword: String ="",
            @SerializedName("FileType") var FileType: String ="",
            @SerializedName("UserId") var UserId: String ="",
            @SerializedName("CountryId") var CountryId: String ="",
            @SerializedName("DateOfBirth") var DateOfBirth: String ="",
            @SerializedName("Mobile") var Mobile: String ="",
            @SerializedName("CountryName") var CountryName: String ="",
            @SerializedName("CompanyName") var CompanyName: String ="",
            @SerializedName("IsAnonymous") var IsAnonymous: String ="",
            @SerializedName("EmployeeIssue") var EmployeeIssue: String ="",
            @SerializedName("DetailedInformation") var DetailedInformation: String ="",
            @SerializedName("EmployeeId") var EmployeeId: String ="",
            @SerializedName("EmployeeEmailId") var EmployeeEmailId: String ="",
            @SerializedName("FCMId") var FCMId: String =""
        )
    

    And called it as

    ApiPost(userName = "ak@gmail.com", password = "12345")