Search code examples
androidkotlinparcel

Write in parcer nullable value in kotlin


Backend returns nullable Int? How should I write nullable value?

 date class Foo (var value: Int?){
   constructor(source: Parcel) : this(
     source.readInt()
   )

   override fun writeToParcel(dest: Parcel, flags: Int) {
     dest.writeInt(value) // doesn't compile - writeInt expect no null value
   }
 }

Now I got solution:

dest.writeInt(value?: -1)

And then check to -1

or write Int like string and then use value of...

but I think it's ugly and wrong.

RESOLVED! My answer:

source.readValue(Int::class.java.classLoader) as Int?,
dest.writeValue(value)

Solution

  • Resolved by:

    source.readValue(Int::class.java.classLoader) as Int?,
    dest.writeValue(value)