【发布时间】:2022-09-30 21:49:45
【问题描述】:
我有下面的代码从列表中选择第一个项目,其中项目的姓氏字段值不应该是UNKNOWN 或MISSING。
val userLastName = someList
.first { it.lastName != \"UNKNOWN\" && it.lastName != \"MISSING\" }
.lastName
现在 Intellij 说字段 userLastName 永远不能为空。为什么?
如果列表中的所有对象的lastName 字段值为UNKNOWN 或MISSING,那么userLastName 变量将为空,对吗?
我试图更改代码以使用 null 安全运算符:
val userLastName = someList
.first { it.lastName != \"UNKNOWN\" && it.lastName != \"MISSING\" }
?.lastName
但我收到以下警告:
Safe call on a non-null receiver will have nullable type in future releases
标签: kotlin