I have next data class in my app:
data class CarouselItem(
val url: String,
val pictureId: String,
val visible: String,
val id : String = UUID.randomUUID().toString()
)
I get from the backend list of CarouselItems. They contain the first 3 fields (url, pictureId and visible). I want to additionally add field id to all created objects and add random unique id value to them. (would like to avoid wrapping this class with another one)
I expected this code to work, but instead, the id is not generated. I also tried adding it like this:
data class CarouselItem(
val url: String,
val pictureId: String,
val visible: String
) {
val id: String = UUID.randomUUID().toString()
}
but it did not help. The id field is still null. To solve this, I added in the code for loop to go through the list and add these values.
I am curious, why is this not working. And is there any way to add these values in the data class? It looks much cleaner like that IMO. Thanks