toBool function

bool toBool(
  1. dynamic object, {
  2. Object? mapKey,
  3. int? listIndex,
})

Converts an object to a bool. mirroring the same static method in the ConvertObject, providing alternative easy less code usage options.

  • Returns true if the object is a bool and equal to true.
  • Returns true if the object is a String and equal to 'yes' or 'true' (case-insensitive).
  • Returns true if the object is a num, int, or double and is larger than zero.
  • Returns false for other types or if the object is null.

object The object to be converted to a bool. mapKey (Optional) Specifies the key to extract values from a Map object. listIndex (Optional) Specifies the index to extract elements from a List object.

Returns a bool, with a default value of false.

Example usage:

final object1 = true;
final bool1 = toBool(object1); // true

final object2 = 'yes';
final bool2 = toBool(object2); // true

final object3 = 10;
final bool3 = toBool(object3); // true

final object4 = false;
final bool4 = toBool(object4); // false

final object5 = 'no';
final bool5 = toBool(object5); // false

final object6 = null;
final bool6 = toBool(object6); // false

Implementation

bool toBool(
  dynamic object, {
  Object? mapKey,
  int? listIndex,
}) =>
    ConvertObject.toBool(object, mapKey: mapKey, listIndex: listIndex);