Dart Tutorial
🇺🇸

Dart Beginner Lesson 19 of 115 2 min read

The Ternary Operator in Dart (condition ? a : b)

Use Dart's ternary operator to choose between two values in a single expression, and know when an if statement reads better.

On this page

The ternary operator is a compact if/else that produces a value.

condition ? valueIfTrue : valueIfFalse

Basic use

void main() {
  var age = 17;
  var status = age >= 18 ? 'adult' : 'minor';
  print(status);
}
minor

The same thing with if takes five lines and needs the variable declared first:

void main() {
  var age = 17;
  String status;
  if (age >= 18) {
    status = 'adult';
  } else {
    status = 'minor';
  }
  print(status);
}

Inside other expressions

Because it is an expression, a ternary can sit anywhere a value can.

void main() {
  var items = 1;
  print('You have $items ${items == 1 ? 'item' : 'items'} in your cart.');

  var stock = 0;
  print(stock > 0 ? 'In stock' : 'Sold out');
}
You have 1 item in your cart.
Sold out

Chaining

You can chain ternaries to pick between more than two values. Lay each branch on its own line so it reads like a table.

void main() {
  var marks = 72;
  var grade = marks >= 80
      ? 'A'
      : marks >= 60
          ? 'B'
          : 'C';
  print(grade);
}
B

With more than two or three branches, a switch expression is clearer.

Ternary or ?? for null checks

When the test is simply “is this null?”, the ?? operator is shorter.

void main() {
  String? nickname;

  var a = nickname != null ? nickname : 'Guest'; // works
  var b = nickname ?? 'Guest';                   // better

  print('$a $b');
}
Guest Guest

When to use it

Use a ternary when you are choosing a value and both options are short. Use if/else when you are doing things, such as calling several functions in each branch. If you have to read a ternary twice to understand it, rewrite it as an if.

Try it yourself

Given var number = 7;, use one ternary to print whether it is even or odd. Then extend it to print zero when the number is 0.

Practise in the playground Updated

Stuck? Look up your error message, check a term in the glossary, or send us your question.