magicBool method

bool magicBool({
  1. bool defaultValue = false,
})

Converts the value to a bool.

  • defaultValue: The value to return if the conversion fails or the value is null.

Implementation

bool magicBool({bool defaultValue = false}) {
  if (isNull) return defaultValue;

  if (this is bool) return this as bool;

  if (this is int) return (this as int) != 0;

  if (this is double) return (this as double) != 0.0;

  if (this is String) {
    final str = (this as String).toLowerCase().trim();
    if (str == 'true' || str == 'yes' || str == '1' || str == 'y') {
      return true;
    }
    if (str == 'false' || str == 'no' || str == '0' || str == 'no') {
      return false;
    }
    return str.isNotEmpty;
  }

  if (this is List) {
    return (this as List).isNotEmpty;
  }

  if (this is Set) {
    return (this as Set).isNotEmpty;
  }

  if (this is Queue) {
    return (this as Queue).isNotEmpty;
  }

  if (this is Map) {
    return (this as Map).isNotEmpty;
  }

  if (this is DateTime) {
    return true;
  }

  log(
    'Unsupported Data Type: ${this?.runtimeType.toString()}',
    name: 'NINJA UI EXTENSION',
  );
  return defaultValue;
}