attributeToBool function

bool attributeToBool(
  1. String? inputValue, {
  2. bool defaultValue = false,
})

A typed version of getDynamic.

If inputValue is a bool, returns it. If inputValue is a String, then '' and 'true' evaluate to true while 'false' evaluates to false. All other string values produce an argument error.

NOTE: inputValue must be non-null. Parses HTML attribute String to a bool.

Should be used to parse values passed to @Attribute constructor argument.

This does not follow the HTML boolean attribute definition (https://stackoverflow.com/a/4139805), as 'false' String will be parsed to false value.

When no attribute is present defaultValue value is returned.

NOTE: no attribute is not the same as no value for attribute:

Implementation

//@Deprecated('Angular now supports boolean properties natively, for @Attribute'
//    ' use [attributeToBool].')
//bool getBool(inputValue) {
//  if (inputValue == null) throw ArgumentError.notNull('inputValue');
//
//  if (inputValue is String) return _parseBool(inputValue);
//  if (inputValue is bool) return inputValue;

//  throw ArgumentError.value(
//      inputValue, 'inputValue', 'Expected a String, or bool type');
//}

/// Parses HTML attribute [String] to a [bool].
///
/// Should be used to parse values passed to @Attribute constructor argument.
///
/// This does not follow the HTML boolean attribute definition
/// (https://stackoverflow.com/a/4139805), as 'false' String will be parsed
/// to false value.
///
/// When no attribute is present [defaultValue] value is returned.
///
/// NOTE: no attribute is not the same as no value for attribute:
///
/// * <my-component foo> - foo attribute is present but has no value, which
///                        is parsed to *true*.
/// * <my-component> - no attribute is present, parsed to [defaultValue].
bool attributeToBool(String? inputValue, {bool defaultValue = false}) {
  if (inputValue == null) return defaultValue;
  return _parseBool(inputValue);
}