asBool method
- bool everythingIsTrue = true,
Converts the current object to a boolean value.
Recognized keywords (case-insensitive):
- 'NULL', 'CANCEL', 'CANCELAR', '', '!', '0', 'FALSE', 'NOT', 'NAO', 'NO', 'NOP', 'DISABLED', 'DISABLE', 'OFF', 'DESATIVADO', 'DESATIVAR', 'DESATIVO', 'N', 'X': Returns
false
. - '1', 'S', 'TRUE', 'YES', 'YEP', 'SIM', 'ENABLED', 'ENABLE', 'ON', 'Y', 'ATIVO', 'ATIVAR', 'ATIVADO', 'OK', 'C': Returns
true
.
If the object doesn't match any of the recognized keywords:
- If
everythingIsTrue
istrue
, returnstrue
. - If
everythingIsTrue
isfalse
, 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
or false
based on the object's representation.
Throws an ArgumentError if the object doesn't represent a valid option and everythingIsTrue
is false
.
Implementation
bool asBool({bool everythingIsTrue = true}) => asNullableBool(everythingIsTrue: everythingIsTrue) ?? false;