parseBool function

bool? parseBool(
  1. Object? v, [
  2. bool? def
])

Parses v to bool.

if v is num: true when v > 0

if v is String: true when [v == "true"|"yes"|"ok"|"on"|"enabled"|"1"|"y"|"s"|"t"|"+"

def The default value if v is invalid.

Implementation

bool? parseBool(Object? v, [bool? def]) {
  if (v == null) return def;

  if (v is bool) return v;

  if (v is num) return v > 0;

  String s;
  if (v is String) {
    s = v;
  } else {
    s = v.toString();
  }

  s = s.trim().toLowerCase();

  if (s.isEmpty) return def;

  return s == 'true' ||
      s == 'yes' ||
      s == 'ok' ||
      s == 'on' ||
      s == 'enabled' ||
      s == 'active' ||
      s == 'activated' ||
      s == 'selected' ||
      s == '1' ||
      s == 'y' ||
      s == 's' ||
      s == 't' ||
      s == '+';
}