TExcelTAD
Home › Dart › Dart Booleans

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

dart · try it yourself
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.

dart · try it yourself
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.

dart · try it yourself
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.

dart · try it yourself
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.

dart · try it yourself
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:

dart
bool? subscribed;              // unknown yet
if (subscribed == true) { }    // only when definitely true
if (subscribed ?? false) { }   // treat null as false

Tip: Avoid if (flag == true) for a plain bool — just write if (flag). Comparing with true is only needed for bool?.

Test yourself

What happens with if (items.length) { ... }?

  1. It runs when length is non-zero
  2. Compile error: an int is not a bool
  3. It always runs
  4. Runtime error

In a() || b(), when is b() NOT called?

  1. When a() returns true
  2. When a() returns false
  3. Never — both always run
  4. When b() returns false

How do you treat a bool? flag that is null as false in a condition?

  1. if (flag)
  2. if (flag!)
  3. if (flag ?? false)
  4. if (bool(flag))