asBoolOrNull method

bool? asBoolOrNull()

Returns the picked value as bool or null if it can't be interpreted as bool.

Only the exact Strings "true" and "false" are valid boolean representations. Other concepts of booleans such as 1 and 0, or "YES" and "NO" are not supported.

Use .let() to parse those custom representations

pick(1).letOrNull((pick) {
   if (pick.value == 1) {
     return true;
   }
   if (pick.value == 0) {
     return false;
   }
   return null;
 });

Implementation

bool? asBoolOrNull() {
  if (value == null) return null;
  try {
    return _parse();
  } catch (_) {
    return null;
  }
}