when Not Null
Execute the code block only if the variable is not null. Inside the function, always use it
to refer to the value of the variable.
For example:
demo.whenNotNull {
// Always use `it` instead of the variable name, since the variable can be null, `it` will never be null if it enters the code block.
Log.d("Check Nullability", it)
}
Content copied to clipboard
It works the same whether it is called with the safe call operator (?) or without it (demo.whenNotNull is equivalent to demo?.whenNotNull). But it is always recommended to call without the safe call operator (demo.whenNotNull { ... })