Write in parcer nullable value in kotlin
Asked Answered
K

3

11

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)
Kindless answered 26/1, 2017 at 12:50 Comment(5)
Wouldn't dest.writeInt(value!!) work? As far as I remember Android doesn't care if you pass a null value into Parcel.Edora
!!- occur nullPointerexception and crash!Kindless
If you found the answer to your question, you should post it as an answer and accept it.Broncho
@Edora writeInt takes an unboxed Java integer which is not nullable. You can't pass null to this method.Midshipmite
Ah, I see. Sorry for misleading @Yvgen. My bad.Edora
K
14

Resolved by:

source.readValue(Int::class.java.classLoader) as Int?,
dest.writeValue(value)
Kindless answered 26/1, 2017 at 21:54 Comment(1)
You can use source.readValue(null) because integer is supported by the system class loader - same in all processes. See Parcel source code.Midshipmite
C
2

This largely depends on what is the semantics of null as a value of your nullable property. It can mean:

  • If value is null then it is absent and should not be written at all:

    value?.let { writeInt(it) }
    

    Of course, the Parcel receiver should be able to determine if it should read the value or not, this should be clear from the values written earlier.

  • value is provided by some code, and it is an error if it is null at the point where it should be written:

    check(value != null) { "value should be not null at the point it's wriiten" }
    writeInt(value!!)
    

    Also, in this case, consider using lateinit var instead of nullable property.

  • Some default value should be used instead of value:

    writeInt(value ?: someDefaultValue())
    

    With Parcel, this does make sense, because otherwise, if the value is missing, you have to specify the fact somewhere else.

  • ... (null can actually mean many things)

Also, this answer shows many idiomatic ways of dealing with nullable values, and you might find some of them useful.

Cheesecake answered 26/1, 2017 at 13:33 Comment(1)
"If value is null then it is absent and should not be written at all" Yes, but we still need to know about this absence when unmarshalling so something has to be written.Midshipmite
T
-1

Use data keyword along with Parcelable,

@Parcelize
data class CallIssue(
                  var name : String? =null,
                  var status: String? = null ,
                  var mobile: String? = null,
                  var priority: Int = 0,
                  var natureOfWork: String? = null,
                  var department: String? = null,
                  var village: String? = null,
                  var time_reported: Date? = null,
                  var reporter: String? = null,
                  var assignee: String? = null,
                  var issue_id: String? = null,
                  var startDate: Date? = null,
                  var endDate: Date? = null,
                  var gender : String?= null,
                  var description: String? = null,
                  var commentList: List<String>? = null,
                  var expanded: Boolean = false) : Parcelable

If you are using @Parcelize then use "data" keyword otherwise all value are null after when you pass data from one activity to other activity.

Ternan answered 22/1, 2020 at 2:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.