Skip to content
AgileLogics
4 min read

Booleans and Comparisons

Prerequisites: Data types and type inference

A boolean (bool) is a yes/no value: true or false. Comparisons produce booleans:

void main() {
  final age = 20;

  print(age > 18);
  print(age == 21);
  print(age != 21);
  print('dart' == 'dart');
  print('apple'.compareTo('banana') < 0);
}
true
false
true
true
true
Operator Meaning
== equal to
!= not equal to
> < greater / less than
>= <= greater / less or equal

Strings don't support < and > in Dart — compare them with compareTo, which returns a negative number, zero, or a positive number.

Only bool is a condition

A condition must be an actual bool. Numbers and strings are never treated as true or false:

void main() {
  final count = 3;
  if (count) {
    print('has items');
  }
}
hello.dart:3:7: Error: A value of type 'int' can't be assigned to a variable of type 'bool'.

Write what you mean: if (count > 0), if (name.isNotEmpty).

Name boolean variables so they read as yes/no questions: isEnabled, hasAccount, canEdit.