Dart if

Summary: in this tutorial, you’ll learn how to use the Dart if statement to execute a code block based on a condition.

Introduction to the Dart if statement

An if statement allows you to do something only if a condition is true. Here’s the syntax of the if statement:

if (condition)
   statement;Code language: Dart (dart)

In this syntax, if the condition is true, the if statement executes the statement. But if the condition is false, the if statement won’t execute the statement.

If you want to execute multiple statements, you need to place them in curly braces like this:

if (expression) {
    // statement;
    // statement;
}Code language: Dart (dart)

It’s a good practice to use a block with the if statement even though it has a single statement.

The following flowchart illustrates how the Dart if statement works:

Dart if

Dart if statement examples

The following examples demonstrate how the if statement works.

1) A simple Dart if statement example

The following example uses the if statement to show a message when the isWeekend is true:

void main() {
  bool isWeekend = true;

  if (isWeekend) {
    print("Let's play!");
  }
}
Code language: Dart (dart)

Output:

Let's play!Code language: Dart (dart)

How it works.

  • First, declare a boolean variable isWeekend with the value true.
  • Second, display the message "Let's play!" if the isWeekend is true. Since the isWeekend is true, the program shows the message.

2) Dart if statement with a condition evaluates to false

The following program doesn’t display anything because the isWeekend is false. Therefore, the if statement doesn’t execute the print statement between the curly braces:

void main() {
  bool isWeekend = false;

  if (isWeekend) {
    print("Let's play!");
  }
}
Code language: Dart (dart)

3) Dart if statement example with a complex condition

In practice, the condition of the if statement this more complex, which consists of multiple expressions like this:

void main() {
  bool isWeekend = true;
  String weather = "sunny";

  if (isWeekend && weather == "sunny") {
    print("Let's go to the park!");
  }
}
Code language: Dart (dart)

Output:

Let's go to the park!Code language: Dart (dart)

Nested Dart if statement

Dart allows you to nest if statements inside an if statement. Here is an example:

void main() {
  bool isWeekend = true;
  String weather = "rainy";

  if (isWeekend) {
    if (weather == "sunny") {
      print("Let's go to the park!");
    }
    if (weather == "rainy") {
      print("Let's play computer game at home!");
    }
  }
}
Code language: Dart (dart)

Output:

Let's play computer game at home!Code language: Dart (dart)

How it works.

  • First, declare the isWeekend and weather variables and initialize their values to true and "rainy".
  • Second, check if the isWeekend is true in the if statement. Because the isWeeknd is true, the if statement executes the statement inside its block.
  • Third, check if the weather is "sunny" in the first nested if statement. Since the weather is "rainy", the first nested if statement does do anything.
  • Finally, check if the weather is "rainy" in the second nested if statment. Because the weather is "rainy", the if statement executes its body that displays the message "Let's play computer game at home!".

Even though you can nest if statements, you should always avoid doing this. Because nested if statements make the code difficult to read and maintain. It’s a good practice to use flat if statements. For example, you can flatten the example above using two if statements like this:

void main() {
  bool isWeekend = true;
  String weather = "rainy";

  if (isWeekend && weather == "sunny") {
    print("Let's go to the park!");
  }
  if (isWeekend && weather == "rainy") {
    print("Let's play computer game at home!");
  }
}
Code language: Dart (dart)

Summary

  • Use the if statement to execute one or more statements when a condition is true.
  • Avoid using nested if statements to make the code easier to read.
Was this tutorial helpful ?