emptyCase method

void emptyCase({
  1. void onEmpty()?,
  2. void onNotEmpty()?,
  3. void onNotEmptyOrNull()?,
})

Handles different cases for a nullable string.

Depending on the state of the source, different callbacks are invoked:

  • If the source is not null and is empty, onEmpty is called.
  • If the source is not null and is not empty, onNotEmpty is called.
  • If the source is null, onNotEmptyOrNull is called.

Parameters:

  • onEmpty: Callback to execute when the source is not null and empty.
  • onNotEmpty: Callback to execute when the source is not null and not empty.
  • onNotEmptyOrNull: Callback to execute when the source is null or not empty.

Example:

'example'.be.emptyCase(
  onEmpty: () => print('The string is empty'),
  onNotEmpty: () => print('The string is not empty'),
  onNotEmptyOrNull: () => print('The string is not empty or null'),
);

Implementation

void emptyCase({
  void Function()? onEmpty,
  void Function()? onNotEmpty,
  void Function()? onNotEmptyOrNull,
}) {
  final value = source;
  if (value != null) {
    if (value.isEmpty) {
      onEmpty?.call();
    } else {
      onNotEmpty?.call();
    }
  } else {
    onNotEmptyOrNull?.call();
  }
}