Dart bool
last modified June 4, 2025
In Dart, bool is a built-in data type that represents boolean values. It can only hold two possible values: true or false. Boolean values are fundamental for control flow and logical operations.
The bool type is commonly used in conditional statements, loops, and logical expressions. Dart provides various operators that work with boolean values, including logical and comparison operators.
Basic bool Declaration
The simplest way to declare a boolean variable is using the bool keyword. Boolean variables can be initialized with true or false literals.
void main() { bool isActive = true; bool isCompleted = false; print('isActive: $isActive'); print('isCompleted: $isCompleted'); }
We declare two boolean variables and print their values. Note that Dart automatically infers the type if we use var, but explicit typing is clearer.
$ dart main.dart isActive: true isCompleted: false
Boolean from Expressions
Boolean values often result from comparison or logical expressions. These expressions evaluate to either true or false.
void main() { int age = 25; bool isAdult = age >= 18; bool canVote = isAdult && age >= 21; print('Is adult: $isAdult'); print('Can vote: $canVote'); }
We create boolean values from comparison (>=) and logical (&&) operations. The expressions evaluate conditions and produce boolean results.
$ dart main.dart Is adult: true Can vote: true
Conditional Statements with bool
Boolean values are primarily used in conditional statements like if-else. The condition must evaluate to a boolean value in Dart.
void main() { bool hasPermission = true; bool isPremiumUser = false; if (hasPermission && !isPremiumUser) { print('Access granted to basic features'); } else if (hasPermission && isPremiumUser) { print('Access granted to all features'); } else { print('Access denied'); } }
We use boolean variables in an if-else chain. The ! operator negates the boolean value. Conditions must be explicit boolean expressions in Dart.
$ dart main.dart Access granted to basic features
Boolean Operators
Dart provides three main logical operators for working with boolean values: && (AND), || (OR), and ! (NOT). These operators follow standard truth tables.
void main() { bool a = true; bool b = false; print('a && b: ${a && b}'); // AND print('a || b: ${a || b}'); // OR print('!a: ${!a}'); // NOT // Short-circuit evaluation bool result = a || (throw Exception('Not evaluated')); print('Short-circuit result: $result'); }
We demonstrate all three boolean operators. Dart uses short-circuit evaluation, so the right operand may not be evaluated if the result is already determined.
$ dart main.dart a && b: false a || b: true !a: false Short-circuit result: true
Type Safety with bool
Dart is type-safe, so only boolean values can be used in boolean contexts. Unlike some languages, Dart doesn't treat other values as "truthy" or "falsy".
void main() { int value = 1; // This would cause a compile-time error: // if (value) { print('Valid'); } // Correct way: if (value != 0) { print('Value is non-zero'); } // Explicit boolean conversion bool isValid = value > 0; print('isValid: $isValid'); }
Dart requires explicit boolean expressions. We must convert non-boolean values using comparison operators before using them in conditions.
$ dart main.dart Value is non-zero isValid: true
Best Practices
- Explicit Types: Prefer bool over var for boolean variables.
- Clear Names: Use names like isActive, hasPermission for booleans.
- Avoid Negations: Positive names make code more readable.
- Parentheses: Use them to clarify complex boolean expressions.
Source
This tutorial covered Dart's bool type with practical examples demonstrating its usage in various contexts. Boolean values are essential for program logic.
Author
List all Dart tutorials.