LetExtensionNullable<T extends Object> extension

Kotlin-style scope functions for nullable types.

These extensions safely handle null values, executing blocks only when the receiver is non-null.

Example

final result = maybeNull?.let((v) => v * 2) ?? 0;
on
  • T?

Methods

also(void block(T it)) → T?

Available on T?, provided by the LetExtensionNullable extension

Executes block for side effects when non-null, returning the original receiver.
let<R>(R block(T it)) → R?

Available on T?, provided by the LetExtensionNullable extension

Executes block when non-null, returning its result or null if the receiver is null.
letNullable<R>(R? block(T? it)) → R?

Available on T?, provided by the LetExtensionNullable extension

Backwards-compatible variant that passes the nullable receiver to block.
letOr<R>(R block(T it), {required R defaultValue}) → R

Available on T?, provided by the LetExtensionNullable extension

Executes block when non-null, returning defaultValue if the receiver is null.
takeIf(bool predicate(T it)) → T?

Available on T?, provided by the LetExtensionNullable extension

Returns the receiver if non-null and satisfies predicate, otherwise null.
takeUnless(bool predicate(T it)) → T?

Available on T?, provided by the LetExtensionNullable extension

Returns the receiver if non-null and does NOT satisfy predicate, otherwise null.