Beginner

Flutter Dart conditions and decisions

If, else, switch or match style branching, guard clauses, and readable decision trees.

Chapter goal: If, else, switch or match style branching, guard clauses, and readable decision trees.

Simple explanation

A condition is a road junction. The program checks a question and chooses which path to follow.

In Flutter Dart, this chapter is about choosing one path from several possible paths. Start with the idea above. Then connect each symbol to a value or action in the example.

Do not try to remember every symbol. First ask what data the program has, what it does with that data, and what result it creates. Technical words become easier when you connect them to those three questions.

Why this topic is important

Programs become useful when they react differently to different data. In Flutter Dart, the syntax may look different from other languages, but the thinking skill transfers: name the data, choose the right operation, and make the next step obvious.

When to use it

Use conditions for permissions, validation, UI states, game rules, and workflow decisions.

Example code

final score = 82;

if (score >= 80) {
  print("strong");
} else if (score >= 50) {
  print("keep practicing");
} else {
  print("restart the lesson");
}

Line-by-line explanation

What the output means

Pass

The output is evidence that the program followed the instructions. If your result is different, read from the first line and write down how each value changes. That is debugging, not failure.

Mistake example

final score = 82;

if (score >= 80) {
  print("strong");
} else if (score > 50) {
  print("keep practicing");
} else {
  print("restart the lesson");
}

The comparison excludes the exact boundary value. A score of 50 should pass, but > 50 rejects it.

Fixed version

final score = 82;

if (score >= 80) {
  print("strong");
} else if (score >= 50) {
  print("keep practicing");
} else {
  print("restart the lesson");
}

The corrected comparison includes the boundary value and sends every expected input to the right branch.

Common mistakes

Warning: Change one part at a time. If you change many lines together, it becomes harder to learn which change caused the result.

Real use cases

Practice exercise

  1. Add a rule for a score below 40.
  2. Add a distinction grade for 80 or above.
  3. Test the boundary values 39, 40, 79, and 80.

Tip: If the exercise feels too large, complete only steps 1 to 3. Small working code teaches more than a large unfinished project.

Mini quiz

  1. Which branch runs when the condition is false?
  2. What boundary value should be tested?
  3. What happens when no branch handles the input?

How to read AI-generated code

Do not copy AI code first. Read it like a detective. Find the data, follow the changes, and locate the final output. Ask AI to explain a line only after you have made your own guess.

Flutter reading check

Find the widget tree first. Then find which values can change, where setState is called, which callback handles the button or TextField, and where navigation or async data enters the screen.

Before you move on

Next topic

Next, learn loops and repetition. Before opening it, explain this chapter out loud in under one minute.

Open the interactive lesson →
← Flutter Dart operators and expressionsFlutter Dart loops and repetition →