Dart bool Data Type by Examples

In this tutorial, we learn how to use bool data type in Dart programming language.

How to declare a bool variable in Dart

Declare true value.

void main() {
  bool value = true;
  
  if(value) {
    print("value is true");
  } else {
    print("value is false");
  }
}
The output is:
value is true

Declare false value.

void main() {
  bool value = false;
  
  if(value) {
    print("value is true");
  } else {
    print("value is false");
  }
}
The output is:
value is false

Convert bool value into String in Dart

void main() {
  bool value1 = true;
  bool value2 = false;

  print(value1.toString());
  print(value2.toString());
}
The output is:
true
false

Basic operators with bool in Dart

The and ( & )operator.

void main() {
  if(true & false) {
    print("true result");
  } else {
    print("false result");
  }
}
The output is:
false result

void main() {
  if(true & true) {
    print("true result");
  } else {
    print("false result");
  }
}
The output is:
true result

void main() {
  if(false & false) {
    print("true result");
  } else {
    print("false result");
  }
}
The output is:
false result

The exclusive or ( ^ ) operator.

void main() {
  if(true^false) {
    print("true result");
  } else {
    print("false result");
  }
}
The output is:
true result

void main() {
  if(true^true) {
    print("true result");
  } else {
    print("false result");
  }
}
The output is:
false result

void main() {
  if(false^false) {
    print("true result");
  } else {
    print("false result");
  }
}
The output is:
false result

The inclusive or ( | ) operator.

void main() {
  if(true|false) {
    print("true result");
  } else {
    print("false result");
  }
}
The output is:
true result

void main() {
  if(true|true) {
    print("true result");
  } else {
    print("false result");
  }
}
The output is:
true result

void main() {
  if(false|false) {
    print("true result");
  } else {
    print("false result");
  }
}
The output is:
false result

Happy Coding 😊

Dart integer Data Type by Examples

Dart double Data Type by Examples

Dart String Data Type by Examples

Dart StringBuffer by Examples

Dart RegExp by Examples

Tags: