I have a data class in Kotlin hat is using the @Parcelize
annotation for easy parcelization. Thing is I now want to pass a function to this class and I do not really know how to make the function not be considered during parceling.
This is my data class:
@Parcelize
data class GearCategoryViewModel(
val title: String,
val imageUrl: String,
val categoryId: Int,
val comingSoon: Boolean,
@IgnoredOnParcel val onClick: (gearCategoryViewModel: GearCategoryViewModel) -> Unit
) : DataBindingAdapter.LayoutViewModel(R.layout.gear_category_item), Parcelable
I tried using @IgnoredOnParcel
and @Transient
without success.
This is the compile error I get:
Error:(20, 39) Type is not directly supported by 'Parcelize'. Annotate the parameter type with '@RawValue' if you want it to be serialized using 'writeValue()'
And this @RawValue
annotation does not work either.
The premise of the question doesn't really make sense.
You can't deserialise this object if you don't serialise all of its properties.
You seem to want
onClick
to benull
if this is deserialised, but even if that was possible, it'd throw a NPE because this property is not marked nullable.Consider changing your architecture so you only serialise simple objects (POJOs or data classes) and not Functions.
I know this isn't exactly a real answer but I would do it like this. Replace your function with class like this:
And use it like this:
Just cast lambda to serializable, and then create object from