Search code examples
kotlinparcelablekotlin-android-extensions

Propertly would not be serialize into a Parcel in Kotlin


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)
    }

Solution

  • 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 {
        ...
    }