Dart Booleans
The bool type has exactly two values: true and false. Booleans drive every decision in a program — if statements, loops, filters and validations.
Boolean values and expressions
void main() {
bool isOpen = true;
var hasStock = false;
var stock = 12;
var isCheap = 8.5 < 10;
var canOrder = isOpen && stock > 0;
print(isOpen);
print(hasStock);
print(isCheap);
print(canOrder);
print(10 == 10.0);
print('a' != 'b');
}true
false
true
true
true
true
No "truthy" values
In JavaScript or Python, 0, '' and empty lists act like false. Dart has no truthiness — conditions must be real bool values. This removes a whole class of subtle bugs.
void main() {
var count = 0;
if (count) {
print('has items');
}
}example.dart:3:7: Error: A value of type 'int' can't be assigned to a variable of type 'bool'.
if (count) {
^
Error: AOT compilation failed
Bad state: Generating AOT kernel dill failed!
Say what you mean instead: count > 0, name.isNotEmpty, list.isEmpty, value != null.
Logical operators
| Operator | Name | Result |
|---|---|---|
!a |
NOT | Flips the value |
a && b |
AND | true only if both are true |
a || b |
OR | true if at least one is true |
a ^ b |
XOR | true if exactly one is true |
&& and || short-circuit: the right side is evaluated only if needed. That makes guards like list.isNotEmpty && list.first > 0 safe.
bool check(String label, bool value) {
print(' evaluating $label');
return value;
}
void main() {
print('AND:');
var a = check('left', false) && check('right', true);
print(a);
print('OR:');
var b = check('left', true) || check('right', false);
print(b);
print('XOR: ${true ^ false}');
}AND:
evaluating left
false
OR:
evaluating left
true
XOR: true
Booleans from methods
Many APIs return bool. Naming boolean variables and getters with is, has, can or should makes conditions read like English.
class Account {
final double balance;
final bool isLocked;
Account(this.balance, {this.isLocked = false});
bool get isOverdrawn => balance < 0;
bool canWithdraw(double amount) => !isLocked && amount <= balance;
}
void main() {
final acc = Account(50);
final frozen = Account(500, isLocked: true);
print(acc.isOverdrawn);
print(acc.canWithdraw(20));
print(acc.canWithdraw(80));
print(frozen.canWithdraw(10));
print('dart'.contains('ar'));
print([1, 2, 3].any((n) => n > 2));
}false
true
false
false
true
true
Parsing booleans
bool.parse converts the strings 'true' and 'false'. bool.tryParse returns null for anything else, and caseSensitive: false relaxes the check.
void main() {
print(bool.parse('true'));
print(bool.tryParse('yes'));
print(bool.parse('FALSE', caseSensitive: false));
}true
null
false
Nullable booleans
A bool? has three states: true, false and null. You can't use it directly in an if; decide what null means:
bool? subscribed; // unknown yet
if (subscribed == true) { } // only when definitely true
if (subscribed ?? false) { } // treat null as falseTip: Avoid
if (flag == true)for a plainbool— just writeif (flag). Comparing withtrueis only needed forbool?.
Test yourself
What happens with if (items.length) { ... }?
- It runs when length is non-zero
- Compile error: an int is not a bool
- It always runs
- Runtime error
In a() || b(), when is b() NOT called?
- When a() returns true
- When a() returns false
- Never — both always run
- When b() returns false
How do you treat a bool? flag that is null as false in a condition?
- if (flag)
- if (flag!)
- if (flag ?? false)
- if (bool(flag))