Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

Dart Tutorial

Dart HOME Dart Intro Dart Get Started Dart Syntax Dart Output Dart Comments Dart Variables Dart final and const Dart Data Types Dart Numbers Dart Strings Dart String Interpolation Dart String Methods Dart Booleans Dart Type Conversion Dart Null Safety Dart late Dart Operators Dart Arithmetic Dart Assignment Dart Comparison Dart Logical Dart Type Test Dart Null Operators Dart Cascade Dart If...Else Dart Switch Dart While Loop Dart For Loop Dart Break/Continue

Dart Collections

Dart Lists Dart Access List Items Dart Change List Items Dart Loop Lists Dart List Methods Dart Sets Dart Maps Dart Access Map Items Dart Loop Maps Dart Collection if/for Dart Spread Operator

Dart Functions

Dart Functions Dart Parameters Dart Named Parameters Dart Return Values Dart Arrow Functions Dart Anonymous Functions Dart Closures Dart Scope Dart Recursion

Dart Classes

Dart OOP Dart Classes/Objects Dart Constructors Dart Named Constructors Dart Factory Constructors Dart Getters and Setters Dart Static Members Dart Inheritance Dart Abstract Classes Dart Interfaces Dart Mixins Dart Extension Methods Dart Enums Dart Generics

Dart 3 Features

Dart Records Dart Patterns Dart Switch Expressions Dart Sealed Classes Dart Class Modifiers

Dart Errors

Dart Exceptions Dart Custom Exceptions

Dart Async

Dart Async Intro Dart Futures Dart async/await Dart Streams Dart Isolates

Dart Libraries

Dart Imports Dart Packages Dart Math Dart Dates Dart JSON Dart RegExp Dart User Input Dart Files Dart Testing

Dart Examples

Dart Examples Dart Exercises


Dart Booleans


Share

Dart Booleans

Very often in programming, you need a value that can only be one of two things, like:

  • YES or NO
  • ON or OFF
  • TRUE or FALSE

For this, Dart has the bool type.

A bool value is either true or false:

Example

void main() {
  bool isDartFun = true;
  bool isFishTasty = false;
  print(isDartFun);
  print(isFishTasty);
}
Try it Yourself »

Result:

true
false

A value of type bool is called a boolean.


Boolean Expressions

A comparison, like "is 10 greater than 9?", gives a boolean as its answer.

This kind of code is called a boolean expression:

Example

void main() {
  print(10 > 9);
  print(10 < 9);
  print(10 == 10);
}
Try it Yourself »

Result:

true
false
true

You can compare variables, and you can store the answer in a variable:

Example

void main() {
  int myAge = 25;
  int votingAge = 18;
  bool canVote = myAge >= votingAge;
  print(canVote);
}
Try it Yourself »

Result:

true

Strings can also be compared with ==:

Example

void main() {
  var password = 'secret';
  print(password == 'secret');
  print(password == 'Secret');
}
Try it Yourself »

Result:

true
false

Learn all comparison operators in the Comparison chapter.



The NOT Operator

The ! operator turns true into false, and false into true:

Example

void main() {
  bool isRaining = true;
  print(!isRaining);
  print(!false);
}
Try it Yourself »

Result:

false
true

You can also join booleans with && (and) and || (or), which you will learn in the Logical chapter.


Booleans in Conditions

Booleans are the base of all decisions in a program.

The if statement runs a block of code only when a boolean is true:

Example

void main() {
  int temperature = 30;
  if (temperature > 25) {
    print('It is a hot day');
  } else {
    print('It is not so hot');
  }
}
Try it Yourself »

Result:

It is a hot day

You will learn more about if in the If...Else chapter.


Only true and false

In some languages, values like 1, 0 or an empty string count as true or false.

Dart does not do this.

A condition must be a real bool, so this program does not run:

Example

void main() {
  int count = 1;
  if (count) {
    print('Count is not zero');
  }
}
Try it Yourself »

Result:

main.dart:3:7: Error: A value of type 'int' can't be assigned to a variable of type 'bool'.
  if (count) {
      ^

Write a comparison instead, so the condition is a boolean:

Example

void main() {
  int count = 1;
  if (count != 0) {
    print('Count is not zero');
  }
}
Try it Yourself »

Result:

Count is not zero

Tip: To turn the text 'true' or 'false' into a boolean, use bool.parse().

Read more in the Type Conversion chapter.



Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.