toBool static method

bool toBool(
  1. dynamic value, {
  2. bool defaultValue = false,
})

Tries to parse value into bool.

null, int, double, bool, String.

If none found, then defaultValue is returned.

Implementation

static bool toBool(dynamic value, {bool defaultValue = false}) {
  if (value is bool) {
    return value;
  }

  if (value == null) {
    return defaultValue;
  }

  if (value is String) {
    return value.toLowerCase() == 'true';
  }

  final num = toInteger(value, defaultValue: -1);

  if (num > -1) {
    return num > 0;
  }

  return defaultValue;
}