The difference between x?.let{ }
and if (x != null) { }
in Kotlin is that the former promotes x
to be non-nullable. Since Kotlin has non-nullable types, it prevents you from needing to write x!!
to assert the non-nullability inside the block.
Dart doesn't have non-nullable types (yet), so that distinction isn't important in Dart.
Just use if (x != null) { ... }
.
If Dart gets non-nullable types, you can be assured that the null
check in the condition will also promote x
to non-nullable inside the block (if x
is a local variable, is not mutated inside the block, other restrictions may apply).
(EDIT: Dart now has nullable types, and x != null
promotes x
to non-null
.)
From your other comments, it sounds like you might be wanting the Kotlin behavior of x?.run { ... }
instead, which binds this
to x
inside the lambda block. There is no corresponding feature in Dart. You cannot override this
, it's always bound to the the object that the current method was called on, even inside nested closures which captures the value of this
just as they capture other variables.
if(variable != null)
?.
operator too. Just no that sort of block yet?
instead of== null
. Note thatif (variable == null) { /* ... */ }
is much easier to read thanvariable?.let { ... }
while it is essentially the same thing.