bool type represents boolean values, restricted to exactly two compile-time constant object instances: true and false. It is the reserved type annotation for the bool class within the dart:core library.
Declaration and Initialization
Variables of typebool can be declared explicitly or inferred by the Dart analyzer.
Type Safety and Coercion
Dart enforces strict boolean safety. Unlike languages that support “truthy” or “falsy” values (such as JavaScript or C), Dart does not perform implicit type coercion. Non-boolean data types, such asint, String, or List, cannot be evaluated as boolean conditions.
Logical Operators
Thebool type supports standard logical operators defined in the language specification.
| Operator | Type | Description | ||
|---|---|---|---|---|
! | Unary Prefix | Logical NOT. Inverts the boolean value. | ||
| ` | ` | Binary | Logical OR. Returns true if at least one operand is true. | |
&& | Binary | Logical AND. Returns true only if both operands are true. |
a || b: Ifaistrue,bis not evaluated.a && b: Ifaisfalse,bis not evaluated.
Nullability
Under Dart’s sound null safety system, thebool type is non-nullable by default. A variable declared as bool must be initialized with true or false before use. To allow a boolean variable to hold a null value, it must be declared as a nullable type bool?.
Core Methods and Properties
Thebool class inherits from Object. While it does not override the equality operator (==), relying instead on Object’s identity comparison, it provides specific implementations for string representation and hashing:
toString(): Returns the string literal “true” or “false”.hashCode: Returns a constant hash code corresponding to the specific boolean instance (trueorfalse).
Master Dart with Deep Grasping Methodology!Learn More





