20

I wish to have my variables that are not from my constructor, part of my Parcelable. However, I face this warning "Propertly would not be serialize into a Parcel", that inform me I can't do that currently.

I am using Kotlin in experimental v.1.2.41.

How can I do ?

@Parcelize
data class MyClass(private val myList: List<Stuff>) : Parcelable {

    val average: List<DayStat> by lazy {
        calculateAverage(myList)
    }
Guillaume agis
  • 3,756
  • 1
  • 20
  • 24

1 Answers1

23

Do you want to make it a part of Parcelable (just mark @Transient) or to write it to the parcel?

In the second case the design document explains why this is a problem (search for "Properties with initializers declared in the class body") and laziness only makes the meaning less clear.

If you can live without laziness, you can do this:

@Parcelize
data class MyClass (private val myList: List<Stuff>, val average: List<DayStat> = calculateAverage(myList)) : Parcelable {
    ...
}
Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487