Skip to main content
The bool type in Dart is a built-in class representing a boolean logic state. Because Dart is a pure object-oriented language, booleans are objects rather than raw primitive data types. A bool is instantiated using one of two compile-time constant literals: true or false. Unlike languages with “truthy” or “falsy” evaluations, Dart enforces strict boolean typing. Control flow statements, conditional expressions, and logical operators strictly require an expression that evaluates to a bool. Dart will not implicitly cast integers, strings, or objects to boolean values; attempting to do so results in a compile-time error.

Constructors and Environment Declarations

The bool class cannot be instantiated via a standard generative constructor. Instead, it provides two constant factory constructors used exclusively for compile-time configuration via environment declarations:
  • bool.fromEnvironment(String name, {bool defaultValue = false}): Returns the boolean value of the environment declaration name.
  • bool.hasEnvironment(String name): Returns true if an environment declaration name exists.
const bool isDebug = bool.fromEnvironment('DEBUG_MODE', defaultValue: false);
const bool hasConfig = bool.hasEnvironment('APP_CONFIG');

Declaration and Null Safety

Under Dart’s sound null safety system, a standard bool is non-nullable. To allow a boolean variable to hold an uninitialized or absent state, it must be explicitly declared as a nullable boolean (bool?).
bool isActive = true;          // Explicitly typed, non-nullable
var isComplete = false;        // Type inferred as bool
bool? isPending = null;        // Nullable boolean

Logical Operators

Dart provides two categories of logical operations for booleans: language-level control flow constructs and operator methods defined directly on the bool class. Language-Level Constructs (Short-Circuiting) The && (logical AND) and || (logical OR) operators are language-level control flow constructs, not methods on the bool class. They perform short-circuit evaluation, meaning the right operand is only evaluated if the left operand does not definitively determine the overall result. The ! operator is a unary prefix operator for logical negation. Class Methods (Non-Short-Circuiting) The bool class explicitly defines three operator methods. Unlike && and ||, these methods evaluate both operands strictly and do not short-circuit:
  • & (Logical AND): Returns true if both operands are true.
  • | (Logical OR): Returns true if either operand is true.
  • ^ (Logical XOR): Returns true if exactly one operand is true.
bool a = true;
bool b = false;

// Language-level short-circuiting constructs
bool shortAnd = a && b; // Evaluates to false (evaluates 'b' because 'a' is true)
bool shortOr  = a || b; // Evaluates to true (short-circuits and ignores 'b')
bool notA     = !a;     // Evaluates to false

// bool class methods (non-short-circuiting, evaluates both operands)
bool strictAnd = a & b; // Evaluates to false
bool strictOr  = a | b; // Evaluates to true
bool strictXor = a ^ b; // Evaluates to true

Parsing Strings to Booleans

Dart provides static methods on the bool class to parse string representations of boolean values. Both methods accept an optional caseSensitive named parameter.
  • bool.parse(String source, {bool caseSensitive = true}): Parses a string into a boolean. By default, it strictly accepts the exact strings 'true' or 'false'. If caseSensitive is set to false, it accepts mixed-case variations. Passing an invalid string throws a FormatException.
  • bool.tryParse(String source, {bool caseSensitive = true}): Operates identically to parse(), but returns null instead of throwing an exception when provided an invalid string.
bool validParse = bool.parse('true');                           // Returns true
bool validFalse = bool.parse('false');                          // Returns false

bool mixedCase = bool.parse('True', caseSensitive: false);      // Returns true
// bool.parse('True');                                          // Throws FormatException
// bool.parse('1');                                             // Throws FormatException

bool? safeParse = bool.tryParse('FALSE', caseSensitive: false); // Returns false
bool? invalidSafe = bool.tryParse('yes');                       // Returns null

Platform Representation

Because Dart compiles to multiple targets, the underlying memory representation of bool depends on the compilation pipeline:
  • Dart Native (AOT/JIT VM): true and false are heap-allocated objects (instances of the VM’s internal Bool class). They are not raw machine primitives in memory, which is why they can be assigned to Object variables. However, the Ahead-Of-Time (AOT) compiler may unbox them locally into machine-level registers during optimization.
  • Dart Web (dart2js/dartdevc): bool maps directly to the underlying JavaScript boolean primitive to ensure zero-overhead interoperability and execution in the browser.
Tired of Poor Dart Skills? Fix That With Deep Grasping!Learn More