androidkotlinparcelablecompanion-object

Property would not be serialized into a 'Parcel'. Add '@IgnoredOnParcel' annotation to remove the warning


Hey I am working in android Kotlin. I am getting this weird warning

Property would not be serialized into a 'Parcel'. Add '@IgnoredOnParcel' annotation to remove the warning

I don't want to add @IgnoredOnParcel this annotation in my class. I want to handle in proper way. I used this How to parcelise member variable other than constructor in data class while using @Parcelize to put my member inside the companion object. I did this correctly but the problem is I cannot access the variable of my data class. Can someone guide me how to get access of my data class variable. Thanks

import android.os.Parcelable
import kotlinx.android.parcel.Parcelize

@Parcelize
data class XYZ(
    val value: String? = null,
) : Parcelable {
    companion object {
        var valueInDouble: Double
            get() {
                return value?.toDoubleOrNull() ?: Double.NaN
            }
    }
}

enter image description here

Getting error on my value variable when I am accessing in my companion object

Unresolved reference: value

Solution

  • Remove the companion object and use val:

    import android.os.Parcelable
    import kotlinx.parcelize.Parcelize
    
    @Parcelize
    data class XYZ(
        val value: String? = null,
    ) : Parcelable {
            val valueInDouble: Double
                get() {
                    return value?.toDoubleOrNull() ?: Double.NaN
                }
    }