How to create empty constructor for data class in Kotlin Android
You have 2 options here:
Assign a default value to each primary constructor parameter:
data class Activity(
var updated_on: String = "",
var tags: List<String> = emptyList(),
var description: String = "",
var user_id: List<Int> = emptyList(),
var status_id: Int = -1,
var title: String = "",
var created_at: String = "",
var data: HashMap<*, *> = hashMapOf<Any, Any>(),
var id: Int = -1,
var counts: LinkedTreeMap<*, *> = LinkedTreeMap<Any, Any>()
)Declare a secondary constructor that has no parameters:
data class Activity(
var updated_on: String,
var tags: List<String>,
var description: String,
var user_id: List<Int>,
var status_id: Int,
var title: String,
var created_at: String,
var data: HashMap<*, *>,
var id: Int,
var counts: LinkedTreeMap<*, *>
) {
constructor() : this("", emptyList(),
"", emptyList(), -1,
"", "", hashMapOf<Any, Any>(),
-1, LinkedTreeMap<Any, Any>()
)
}
If you don't rely on copy
or equals
of the Activity
class or don't use the autogenerated data class
methods at all you could use regular class like so:
class ActivityDto {
var updated_on: String = "",
var tags: List<String> = emptyList(),
var description: String = "",
var user_id: List<Int> = emptyList(),
var status_id: Int = -1,
var title: String = "",
var created_at: String = "",
var data: HashMap<*, *> = hashMapOf<Any, Any>(),
var id: Int = -1,
var counts: LinkedTreeMap<*, *> = LinkedTreeMap<Any, Any>()
}
Not every DTO needs to be a data class
and vice versa. In fact in my experience I find data classes to be particularly useful in areas that involve some complex business logic.
In Kotlin, how do you declare a data class with zero constructor parameters?
Because data class
without data doesn't make sense. Use object
for singletons:
object Nil : IntList()
How to create constructor for data class in Kotlin Android?
For a data class, the constructor defined in the class header is the primary constructor. It can't delegate to any other constructor in that class (though it can delegate to a superclass constructor).
However, you can define secondary constructors in the body of the class, as long as those delegate to the primary constructor. For example:
data class EventItem(val dateEvent: String) {
constructor(date: Date) : this(date.toString())
}
The problem in the code you've posted is that your secondary constructor is trying to delegate back to itself. You have to delegate to the primary constructor instead, and that means you need to be able to determine a value for all of the parameters missing from the secondary constructor.
Can Kotlin data class have more than one constructor?
you can set the data class like this
data class User(val name: String? = null, val id: String? = null, val email: String? = null)
and you can instance the object with multiple constructors like this
val userId = User(id = "123456")
val userMail = User(email= "email@email.com")
val userName = User("Name")
Searching a workaround for kotlin empty data class primary constructor
Just change FooEvent
to a normal class, and add (or generate them using your IDE) toString()
, hashCode()
and equals(Object)
if needed:
class FooEvent: Event() {
override hashCode() = ...
override equals(other: Object) {
...
}
override toString() = ...
}
To make the event a data class, simply add an unused property to it. Not pretty, but as short as it can be in Kotlin at the moment:
data class FooEvent(val dummy: Unit = Unit) : Event()
There seems to be no intention to remove this limitation soon:
- Data class without arguments deprecated in 1.0. Why?
- Suggestion for parameterless data class
Empty constructor for kotlin object to use Firebase
Use default arguments:
class Video(var id: String = "", var url: String = "",
var owner: User = User("", "", ""))
(also, consider using val
).
Related Topics
How to Add a Badge to an Application Icon in Android
Listview with Clickable/Editable Widget
How to Get String from Different Locales in Android
Do I Need All Three Constructors for an Android Custom View
Detect from Browser If a Specific Application Is Installed in Android
Detect a New Android Notification
How to Get the Sender of an Intent
How to Create and Read Log on Android Devices
Letting Webview on Android Work with Prefers-Color-Scheme: Dark
Android Dialogfragment VS Dialog
Difficulties with Calling an Android Ndk Function from Directly Delphi
Playing a Video in Videoview in Android