The Boolean data type (bool) is the most basic yet powerful type in programming. It has only two possible values: true or false. Booleans are the decision-makers in your code, controlling the flow of logic based on conditions.
1. Declaring Booleans
Booleans are strictly typed in Dart. Unlike some other languages where 1 or 0 might count as true/false, in Dart, a boolean must explicitly be the keyword true or false.
Basic Declaration
1// Explicit declaration2bool isOn = true;3bool isOff = false;4 5// Type inference (common in Dart)6var isLoading = true;7var hasError = false;8 9print(isOn); // true10print(isOff); // false11print(isOn.runtimeType); // boolHintA boolean variable stores the state of a condition. It is either on or off, yes or no.
2. Comparison Operators
Booleans are often the result of comparing two values. Dart uses standard comparison operators to evaluate relationships between numbers or other data.
Making Comparisons
1int score = 100;2 3// Equality operators4print(score == 100); // true (Equal to)5print(score != 50); // true (Not equal to)6 7// Relational operators8print(score > 50); // true (Greater than)9print(score < 50); // false (Less than)10print(score >= 100); // true (Greater than or equal to)11print(score <= 90); // false (Less than or equal to)12 13bool isWinner = score > 80;14print('Is Winner: $isWinner'); // Is Winner: trueHintThe result of any comparison (like 5 > 3) is always a boolean value.
3. Logical Operators
You can combine multiple boolean conditions using logical operators: AND (&&), OR (||), and NOT (!).
Combining Logic
1bool hasLicense = true;2bool isOver18 = false;3 4// AND (&&): Both must be true5bool canDrive = hasLicense && isOver18;6print('Can drive: $canDrive'); // false7 8// OR (||): At least one must be true9bool needsRide = !hasLicense || !isOver18; 10print('Needs ride: $needsRide'); // true11 12// NOT (!): Inverts the value13bool isMinor = !isOver18;14print('Is minor: $isMinor'); // trueHintUse && when you need strict compliance (all conditions met). Use || when any single condition is enough.
4. Common "Gotchas"
A classic mistake in many languages is confusing the assignment operator (=) with the equality operator (==).
Assignment vs. Equality
1int x = 10;2int y = 5;3 4// Correct: Comparing x and y5bool isEqual = (x == y); // false6 7// Incorrect logic often seen:8// if (x = y) ... // ❌ Error in Dart!9// Dart prevents this because 'x = y' is an assignment, not a boolean expression.10 11print('Is x equal to y? $isEqual');HintRemember: = sets a value. == checks a value.
Loot collected
Key takeaways- The
booltype only acceptstrueorfalse. - Dart is type-safe: numbers (1, 0) are NOT automatically converted to booleans.
- Comparison operators (
==,!=,>,<) return boolean results. - Logical operators allow you to combine conditions:
&&(AND),||(OR),!(NOT). - Don’t confuse
=(assignment) with==(equality check).
Side quests
Practice exercises
Exercise 1: Simple Logic
Create a boolean variable called isSunny. Set it to true. Print "Is it sunny? " followed by the variable.
Reveal solutionHide solution
1bool isSunny = true; print('Is it sunny? $isSunny');Exercise 2: The Night Club Bouncer
Declare two variables: age (int) and hasID (bool). Create a boolean canEnter that is true ONLY if age is 21 or older AND hasID is true.
Reveal solutionHide solution
1int age = 20; bool hasID = true; bool canEnter = (age >= 21) && hasID; print(canEnter);Exercise 3: Opposite Day
Given bool isHappy = true;, use the NOT operator to print false.
Reveal solutionHide solution
1print(!isHappy);