Colton Idle
11/17/2021, 2:02 AMdata class Person(
val name: String = "",
)
but if firestore sends null
, then even though the type is non-null, if I put a breakpoint... I can see that I do actually get a null value?
Question: How can a non-null property like name
be null? I'm assuming this is because of reflection/java-interop or something?Stephan Schroeder
11/17/2021, 7:35 AMString!
. You can't create that type in Kotlin, only by java interop, and the thing is: the compiler will trust you, when you say it's not nullable! So
val platformStr: String! = someJavaFunction()
val nullableStr: String? = someKotlinFunction()
val s1: String = nullableStr // Compiler error! can't assign a nullable variable to a non-nullable one
val s2: String = platformStr // no Compiler error, but maybe still null!!! but the compiler trusts you to have more knowledge about the code then itself can deduce
So if you invoke java code and sometimes it returns null
then set the type as nullable, so you're forced to handle the null-case.
You can also check other articles on this topic, like this one https://p5v.medium.com/platform-types-in-kotlin-5caceeb556ad