Bool is a boolean data type in Dart language.
It is used to store two values true
or false
values. These are also called compile-time boolean
constants.
true
and false
values are reserved words, that are not used as an identifier for variable functions, or class names in Dart language.
The Bool
type is used in the following cases.
- Variable can be declared with these types to store
true
orfalse
conditional expressions. - Dart expressions are evaluated as
true
orfalse
in conditional expressions such as if statements. - Also used dart operator produces bool values
How to Declare bool variable?
Variables are declared like normal variables in Dart. The default value for the boolean type is null. Boolean values are declared and assigned with values on the same line.
bool variable_name=true;
bool variable_name1=false;
variable_name
is a valid identifier in the dart
programming language.
true
and false
values are the only values that are assigned to the bool type.
If you assign with any other values, It throws Error: Undefined name
.
The following are invalid bool types.
bool flag='True';
bool flag='true';
Error: A value of type ‘int’ can’t be assigned to a variable of type ‘bool’.
bool flag=1;
How do you write a Boolean in darts?
Boolean is a predefined inbuilt data type in a Dart programming language. It stores true and false values only.
Here is an example
void main() {
bool? flag;
print(flag);
}
Bool value can be used in a conditional expression.
Here is an if-conditional expression example
void main() {
var flag = true;
if (flag) {
print("Condition satisfied.");
} else {
print("Condition not satisfied.");
}
}
Similarly, You can combine boolean variables with logical boolean (&&,||,!)operators.
void main() {
var flag = true;
print(true || false); // true
print(true && false); //false
print(true && true); //true
}