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)
Yvgen
  • 2,036
  • 2
  • 23
  • 27

3 Answers3

14

Resolved by:

source.readValue(Int::class.java.classLoader) as Int?,
dest.writeValue(value)
Eugen Martynov
  • 19,888
  • 10
  • 61
  • 114
Yvgen
  • 2,036
  • 2
  • 23
  • 27
  • You can use `source.readValue(null)` because integer is supported by the system class loader - same in all processes. See [`Parcel` source code](https://cs.android.com/android/platform/superproject/+/master:frameworks/base/core/java/android/os/Parcel.java;l=2854;drc=master). – Eugen Pechanec Jul 23 '20 at 11:41
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.

Community
  • 1
  • 1
hotkey
  • 140,743
  • 39
  • 371
  • 326
  • 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. – Eugen Pechanec Jan 26 '17 at 13:44
-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.

Alok Gupta
  • 1,806
  • 22
  • 21