https://kotlinlang.org logo
#android
Title
# android
m

martynas

06/27/2018, 6:39 AM
How can I exclude value from
@Parcelize
? I have a class where
Bar
is 3rd party library class.
Copy code
data class Foo(
  val id : Int = 0,
  val bar : Bar? = null
)
At compile time I get
Type is not directly supported by 'Parcelize'. Annotate the parameter type with '@RawValue' if you want it to be serialized using 'writeValue()'
on
Bar
. That is fine by me. I’m happy to lose that value. But no combination of
@IgnoredOnParcel
and
@RawValue
did not worked for me yet - runtime error when serializing value.
l

Lucas Ł

06/27/2018, 7:21 AM
It's probably not exactly what you need, but you could do something like this:
Copy code
@Parcelize
data class Foo(val id: Int = 0) : Parcelable {
    @IgnoredOnParcel
    var bar: Bar? = null
}
The error doesn't appear, even without the
@IgnoredOnParcel
annotation or just do it the old fashioned way, and implement the
Parcelable
interface yourself 🙂
m

martynas

06/27/2018, 8:34 AM
Yeah that would, but then I have to make
bar
mutable
Which is not desirable
Worked around it by implementing
Parceler
.
Copy code
NoOpParceler : Parceler<Any?> {
    override fun create(parcel: Parcel): Any? = null
    override fun Any?.write(parcel: Parcel, flags: Int) = Unit
}
now it works with any “undesirable” 🙂
👍 1
k

kenkyee

06/27/2018, 1:05 PM
I filed a ticket for @IgnoredOnParcel... Doesn't work
👏 1
m

martynas

06/27/2018, 4:43 PM
@kenkyee could you share a link to the ticket?
comment on it w/ your use case to make sure it's accounted for
m

martynas

06/28/2018, 6:47 AM
I see, that Yan commented about my case
It’s not supposed to work on constructor parameters
l

Lucas Ł

06/28/2018, 6:49 AM
I guess I was pretty close with my snippet to the intended use 😂
simple smile 1
k

kenkyee

06/28/2018, 9:39 AM
I still don't understand why it can't work on constructor parameters, especially with data classes...
354 Views