属性不会被序列化为 Kotlin 中的包裹

Propertly would not be serialize into a Parcel in Kotlin

我希望我的变量不是来自我的构造函数,是我的 Parcelable 的一部分。 但是,我遇到了这个警告 "Propertly would not be serialize into a Parcel",告诉我目前我不能这样做。

我在 experimental v.1.2.41.

中使用 Kotlin

我该怎么办?

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

    val average: List<DayStat> by lazy {
        calculateAverage(myList)
    }

您是要将其作为Parcelable的一部分(只需标记@Transient)还是将其写入包裹?

在第二种情况下 the design document 解释了为什么这是一个问题(搜索 "Properties with initializers declared in the class body"),懒惰只会让意思不那么清楚。

如果你可以不懒惰地生活,你可以这样做:

@Parcelize
data class MyClass (private val myList: List<Stuff>, val average: List<DayStat> = calculateAverage(myList)) : Parcelable {
    ...
}