Search code examples
kotlinnullable

what does "??" mean in Kotlin


I am a student studying about mvvm.

And I saw some code.

so what does "??" mean in kotlin.

I searched for this but I couldn't find it.

How can I fix it if I'm wrong? -> (s.toString ?? "")

1.

fun wrap(a: Int?): Int?? {
    return a
}

fun desc(a: Int??) {
    if (a == null) {
        println("None")
    } else {
        if (a == null) {
            println("Some(None)")
        } else {
            println("Some(Some($a))")
        }
    }
}

fun main(args: Array<String>) {
    val a: Int?? = wrap(null)
    desc(a) 
}

2.

<layout/>
    <data>
        <variable>
            name="signid"
            type="SignidViewModel" />
    </data>

    <EditText
        android:onTextChanged='@{(s,start,end,before) -> signid.Check_ID(s.toString ?? "")}' />
</layout>

Solution

  • The null safety operator is defined as '?' in kotlin. If you are talking about null-aware operator in dart then in that case two question marks is used. The null-awareness in kotlin is achieved making null-safe to variable as

    val testString:String?

    In dart (or any other language) this is called for the null check as

    x=y ?? z

    meaning, assing y to x until y is not null, else z