Skip to main content
The 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 type bool can be declared explicitly or inferred by the Dart analyzer.
// Explicit declaration
bool isActive = true;

// Type inference (var)
var isComplete = false; // Inferred as bool

// Compile-time constants
const bool isDebug = true;
final bool isLoaded = false;

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 as int, String, or List, cannot be evaluated as boolean conditions.
var count = 0;

// Compile-time error: The type 'int' can't be used as 'bool' in a condition.
// if (count) { ... } 

// Correct: Explicit comparison required
if (count != 0) { 
  // ...
}

Logical Operators

The bool type supports standard logical operators defined in the language specification.
OperatorTypeDescription
!Unary PrefixLogical NOT. Inverts the boolean value.
``BinaryLogical OR. Returns true if at least one operand is true.
&&BinaryLogical AND. Returns true only if both operands are true.
Short-Circuit Evaluation: Dart employs short-circuit evaluation for binary logical operators:
  • a || b: If a is true, b is not evaluated.
  • a && b: If a is false, b is not evaluated.

Nullability

Under Dart’s sound null safety system, the bool 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?.
bool isValid = true;      // Can only be true or false
bool? isPending;          // Can be true, false, or null

isPending = null;         // Valid
// isValid = null;        // Compile-time error

Core Methods and Properties

The bool 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 (true or false).
bool flag = true;
String text = flag.toString(); // "true"
Master Dart with Deep Grasping Methodology!Learn More