ConditionExtension<T> extension

Extension on T? to add condition-based methods.

This extension provides a set of methods that allow for condition-based operations on nullable types. These methods include conditionNotNullWith and conditionNotNullAs, which execute one of two functions based on the nullability of the value and/or a condition, and return a value of a specified type.

These methods are useful when you need to perform different operations and return a result based on the nullability of a value and/or a condition.

Example usage:

var nullableInt = 1;
var result = nullableInt.conditionNotNullWith<int>(
  isTrue: (item) => item * 2,
  isFalse: () => 0,
);
print(result); // prints: 2

result = nullableInt.conditionNotNullAs<int>(
  condition: (item) => item > 1,
  isTrue: (item) => item * 2,
  isFalse: () => 0,
);
print(result); // prints: 0

Generic type T can be any type and is used for the type of the nullable value.

on
  • T?

Methods

conditionNotNullAs<R>({bool condition(T)?, required R isTrue(T), required R isFalse()}) → R

Available on T?, provided by the ConditionExtension extension

Executes one of two functions based on a condition and returns a value of type R.
conditionNotNullWith<R>({required R isTrue(T), required R isFalse()}) → R

Available on T?, provided by the ConditionExtension extension

Executes one of two functions based on the nullability of the value and returns a value of type R.
ifNull(void action()) → T?

Available on T?, provided by the ConditionExtension extension

Executes the action if the value is null.
notNullWith<R>({required R isTrue(T), required R isFalse()}) → R

Available on T?, provided by the ConditionExtension extension

Executes one of two functions based on the nullability of the value and returns a value of type R.