后端返回可为空的Int?

 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
   }
 }

现在我有办法了:

dest.writeInt(value?: -1)

然后 Select -1

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

但我认为这是丑陋和错误的.

RESOLVED! My answer:

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

推荐答案

Resolved by:

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

Kotlin相关问答推荐

在kotlin中使用List(mylist. size){index—TODO()}或Map迭代>

等待下一个值时暂停Kotlin Coroutine

Spring Boot Bean验证器未触发

如何接受任何派生类KClass

T和T有什么区别:任何>

如何使用 Kotlin Maven 更改 Minecraft 插件中的 Shulker GUI 标题

Kotlin 复制列表中的项目以创建具有相同数据的不同对象的新列表

如何将消费者放入 Kotlin 的 map 中?

Kotlin 可空泛型

Jetpack Compose:当状态从另一个活动改变时强制重组

嵌套数组 indexOf()

Kotlin:查找集合中最常见的元素

Koin Android:org.koin.error.NoBeanDefFoundException

Anko 中的水平线性布局

片段内的 Kotlin 按钮 onClickListener 事件

无法为 retrofit2.Call 调用无参数构造函数

Kotlin Android:属性委托必须有一个 'getValue(DashViewModel, KProperty*>)' 方法

保存对象时未填充 Spring Boot JPA@CreatedDate @LastModifiedDate

Android Jetpack Compose 中的文本渐变

类型推断失败:RecyclerViewActions.scrollTo()