asBool method

bool asBool({
  1. 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 is true, returns true.
  • If everythingIsTrue is false, throws an ArgumentError.

Example usage:

bool result = someObject.asNullableBool(everythingIsTrue: false);
  • everythingIsTrue: A boolean flag indicating whether to treat all non-null values as true. If set to true, any non-null value (except specific keywords) will be considered true. If set to false, 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;