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
Thebool 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 declarationname.bool.hasEnvironment(String name): Returnstrueif an environment declarationnameexists.
Declaration and Null Safety
Under Dart’s sound null safety system, a standardbool 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?).
Logical Operators
Dart provides two categories of logical operations for booleans: language-level control flow constructs and operator methods defined directly on thebool 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): Returnstrueif both operands aretrue.|(Logical OR): Returnstrueif either operand istrue.^(Logical XOR): Returnstrueif exactly one operand istrue.
Parsing Strings to Booleans
Dart provides static methods on thebool 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'. IfcaseSensitiveis set tofalse, it accepts mixed-case variations. Passing an invalid string throws aFormatException.bool.tryParse(String source, {bool caseSensitive = true}): Operates identically toparse(), but returnsnullinstead of throwing an exception when provided an invalid string.
Platform Representation
Because Dart compiles to multiple targets, the underlying memory representation ofbool depends on the compilation pipeline:
- Dart Native (AOT/JIT VM):
trueandfalseare heap-allocated objects (instances of the VM’s internalBoolclass). They are not raw machine primitives in memory, which is why they can be assigned toObjectvariables. However, the Ahead-Of-Time (AOT) compiler may unbox them locally into machine-level registers during optimization. - Dart Web (dart2js/dartdevc):
boolmaps directly to the underlying JavaScriptbooleanprimitive to ensure zero-overhead interoperability and execution in the browser.
Tired of Poor Dart Skills? Fix That With Deep Grasping!Learn More





