toBool static method
Converts an object to a bool
.
- Returns
true
if the object is abool
and equal totrue
. - Returns
true
if the object is aString
and equal to 'yes' or 'true' (case-insensitive). - Returns
true
if the object is anum
,int
, ordouble
and is larger than zero. - Returns
false
for other types or if the object isnull
.
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 = ConvertObject.toBool(object1); // true
final object2 = 'yes';
final bool2 = ConvertObject.toBool(object2); // true
final object3 = 10;
final bool3 = ConvertObject.toBool(object3); // true
final object4 = false;
final bool4 = ConvertObject.toBool(object4); // false
final object5 = 'no';
final bool5 = ConvertObject.toBool(object5); // false
final object6 = null;
final bool6 = ConvertObject.toBool(object6); // false
Implementation
static bool toBool(
dynamic object, {
dynamic mapKey,
int? listIndex,
}) {
if (mapKey != null && object is Map<dynamic, dynamic>) {
return (object[mapKey] as Object?).asBool;
}
if (listIndex != null && object is List<dynamic>) {
return (object.of(listIndex) as Object?).asBool;
}
return (object as Object?).asBool;
}