gpt4 book ai didi

android - 为什么 Android Studio 指示一个对象可能为 null 而它不能为 null?

转载 作者:搜寻专家 更新时间:2023-11-01 07:42:01 26 4
gpt4 key购买 nike

这是我正在使用的代码:

ref.get().addOnCompleteListener(task -> {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
Log.d(TAG, "DocumentSnapshot data: " + document.getData());
}
}
});

这就是 Android Studio 突出显示错误的方式:

enter image description here

据我了解answer :

A successfully completed task will never pass null for the DocumentSnapshot.

如何在不检查无效性的情况下解决这个问题?谢谢!

最佳答案

Why does Android Studio indicate that an object might be null when it cannot be?

因为 lint 无法知道。 document.exists() 通常会返回 null,这就是您看到警告的原因。 Lint 执行基本检查并且不知道有关 Firebase API 的详细信息。您甚至可以使用 Java Core API 重现此行为。

How can I solve this without checking for nullity? Thanks!

如果您 100% 确定它永远不会为 null,则可以使用

@SuppressWarnings("ConstantConditions")

但我不建议这样做,因为您不知道 Firebase API 将来是否会发生变化。也许下一个版本的 Firebase 将允许返回 null。

关于android - 为什么 Android Studio 指示一个对象可能为 null 而它不能为 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54925393/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com