asNullableBool method
- bool everythingIsTrue = true,
Converts the current object to a nullable boolean value.
If the object is null, this function returns null.///
Recognized keywords (case-insensitive):
- 'NULL', 'CANCEL', 'CANCELAR','ABORT', 'ABORTAR': Returns
null. - '', '!', '0', 'FALSE', 'NOT', 'NAO', 'NO', 'NOP', 'DISABLED', 'DISABLE', 'OFF', 'DESATIVADO', 'DESATIVAR', 'DESATIVO', 'N','X','E','ERRADO', 'FALSO', 'MENTIRA' : Returns
false. - '1', 'S', 'TRUE', 'YES', 'YEP', 'SIM', 'ENABLED', 'ENABLE', 'ON', 'Y', 'ATIVO', 'ATIVAR', 'ATIVADO', 'OK','C','VERDADE','VERDADEIRO','CORRETO','CERTO': Returns
true.
If the object doesn't match any of the recognized keywords:
- If
everythingIsTrueistrue, returnstrue. - If
everythingIsTrueisfalse, throws an ArgumentError.
Example usage:
bool? result = someObject.asNullableBool(everythingIsTrue: false);
everythingIsTrue: A boolean flag indicating whether to treat all non-null values astrue. If set totrue, any non-null value (except specific keywords) will be consideredtrue. If set tofalse, an exception will be thrown for unrecognized values.
Returns true, false, or null based on the object's representation.
Throws an ArgumentError if the object doesn't represent a valid option and everythingIsTrue is false.
Implementation
bool? asNullableBool({bool everythingIsTrue = true}) {
if (this == null) return null;
if (this is bool) return this as bool;
var x = '$this'.toUpperCase().removeDiacritics.trimAll;
switch (x) {
case 'NULL':
case 'CANCEL':
case 'CANCELAR':
case 'ABORT':
case 'ABORTAR':
return null;
case '':
case '!':
case '0':
case 'FALSE':
case 'NOT':
case 'NAO':
case 'NO':
case 'NOP':
case 'DISABLED':
case 'DISABLE':
case 'OFF':
case 'DESATIVADO':
case 'DESATIVAR':
case 'DESATIVO':
case 'N':
case 'X':
case 'F':
case 'FALSO':
case 'MENTIRA':
case 'ERRADO':
case 'E':
return false;
case '1':
case 'S':
case 'TRUE':
case 'YES':
case 'YEP':
case 'SIM':
case 'ENABLED':
case 'ENABLE':
case 'ON':
case 'Y':
case 'ATIVO':
case 'ATIVAR':
case 'ATIVADO':
case 'OK':
case 'C':
case 'V':
case 'VERDADEIRO':
case 'VERDADE':
case 'CERTO':
case 'CORRETO':
return true;
default:
return everythingIsTrue ? true : throw ArgumentError('The object does not represent a valid option and the EverythingIsTrue flag is set to false.');
}
}