Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Kotlin 检查Null后显示Null安全错误_Kotlin_Kotlin Null Safety - Fatal编程技术网

Kotlin 检查Null后显示Null安全错误

Kotlin 检查Null后显示Null安全错误,kotlin,kotlin-null-safety,Kotlin,Kotlin Null Safety,我执行一个函数(listOf(matchUid,first_name,gender,matchBio,age)。任何{it==null})检查传入的变量是否为null: private fun getMatchData(doc: DocumentSnapshot){ val matchUid = if (isUser1) doc.getString("user2") else doc.getString("user1") val first_name = if (isUser1)

我执行一个函数(
listOf(matchUid,first_name,gender,matchBio,age)。任何{it==null}
)检查传入的变量是否为
null

private fun getMatchData(doc: DocumentSnapshot){
    val matchUid = if (isUser1) doc.getString("user2") else doc.getString("user1")
    val first_name = if (isUser1) doc.getString("user2name") else doc.getString("user1name")
    val gender = if (isUser1) doc.getString("user2gender") else doc.getString("gender")
    val matchBio = if (isUser1) doc.getString("user2bio") else doc.getString("user1bio")
    if ( listOf(matchUid, first_name, gender, matchBio, age).any { it == null } ) return goOffline()
    if (matchUid == null) return goOffline()
    if (!isUser1) Group = Group().apply {
        id = doc.id
        user1 = matchUid
        user2 = user.uid
        match = User(matchUid, first_name, gender, null, true)
    } 
即使它检查了这一点,
first_name
gender
由于空安全性,在编译器中有红色下划线
matchUid
没有红线,因为我在下面的一行中显式地检查了null


为什么在我已经检查过它之后,编译器仍然给出空警告?

所以,问题是编译器不够聪明,或者。。。我们没有提供足够的信息

在您的情况下,确保
firstName
gender
不为空的有问题的调用是:

if (listOf(matchUid, firstName, gender, matchBio, age).any { it == null }) return goOffline()
如果将其更改为简单的空值链,它将正常工作:

if (matchUid == null || firstName == null || gender == null || matchBio == null || age == null) return goOffline()
那为什么呢?编译器只是不知道
listOf(vararg对象:Any?)。Any{it==null}
意味着这些对象都不是null。 那么,我们能做什么

Kotlin 1.3为我们编写
合同提供了很大的可能性,这是编译器的一个技巧,例如,如果
f(x)
返回
true
意味着
x
不为空。但是,不幸的是,契约不支持varargs参数(或者我还没有找到一种方法)


因此,在您的情况下,您可以用单空检查链替换您的调用。

所以,问题是编译器不够智能,或者。。。我们没有提供足够的信息

在您的情况下,确保
firstName
gender
不为空的有问题的调用是:

if (listOf(matchUid, firstName, gender, matchBio, age).any { it == null }) return goOffline()
如果将其更改为简单的空值链,它将正常工作:

if (matchUid == null || firstName == null || gender == null || matchBio == null || age == null) return goOffline()
那为什么呢?编译器只是不知道
listOf(vararg对象:Any?)。Any{it==null}
意味着这些对象都不是null。 那么,我们能做什么

Kotlin 1.3为我们编写
合同提供了很大的可能性,这是编译器的一个技巧,例如,如果
f(x)
返回
true
意味着
x
不为空。但是,不幸的是,契约不支持varargs参数(或者我还没有找到一种方法)


因此,在您的情况下,您可以用单空检查链替换您的调用。

我认为这是因为编译器不够聪明。我想说的是,编译器不知道您正在对列表做什么,也不知道您是否可能将元素映射到其他内容。要做到这一点,需要对代码进行一定程度的分析,这可能超出了大多数编译器的范围。如果你不知道
listOf(…)
到底在做什么,即使你是人,也可能无法知道实际发生了什么。我猜是因为编译器不够聪明。我想编译器不知道你在用列表做什么,也不知道你是否可能将元素映射到其他东西。要做到这一点,需要对代码进行一定程度的分析,这可能超出了大多数编译器的范围。如果你不知道(…)
列表到底在做什么,即使你是人类也可能无法知道到底发生了什么。