这是我正在使用的代码:
ref.get().addOnCompleteListener(task -> {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
Log.d(TAG, "DocumentSnapshot data: " + document.getData());
}
}
});
Android Studio 是如何突出显示错误的:
据我了解,answer :
A successfully completed task will never pass
null
for theDocumentSnapshot
.
如何在不检查无效性的情况下解决这个问题?谢谢!
最佳答案
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%确定它永远不会为空,你可以使用
@SuppressWarnings("常量条件")
但我不建议这样做,因为您不知道 Firebase API 将来是否会发生变化。也许 Firebase 的下一版本将允许 null 返回。
关于android - 为什么 Android Studio 指示对象可能为 null,但实际上它不能为 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54925393/