dart-control-flujo-enums

Flow Control and Enums in Dart

  • 4 min

The flow control is about deciding which instructions to execute and which to repeat.

So far, our programs have been a straight line: they start at the top and end at the bottom. But real software needs to make decisions and repeat tasks.

In this post we are going to look at the flow control structures. If you come from C, Java, or JavaScript, the syntax will feel familiar. Pay special attention to enums, because we will use them frequently when building interfaces in Flutter.

Conditionals for making decisions

The basic structure for decision-making is if and else.

if and else

It works exactly the same as in most C-style languages.

int battery = 15;

if (battery > 20) {
  print("All good");
} else if (battery > 5) {
  print("Power saving mode activated");
} else {
  print("Shutting down system...");
}
Copied!

The ternary operator

In the Flutter ecosystem, the ternary operator appears constantly. It allows us to simplify an if-else into a single line. It is very useful for displaying one Widget or another in the user interface.

The syntax is: condition ? value_if_true : value_if_false.

bool isAdmin = true;
String message = isAdmin ? "Welcome Admin" : "Welcome User";
Copied!

The ternary operator frequently appears in expressions that build the Flutter UI. Use it for brief conditions; if the logic grows, it is clearer to extract it or use a regular if.

Enums: enumerated types

Before looking at the switch, we need to talk about Enums.

An enum (enumeration) is a type used to represent a fixed set of constant values.

Imagine you want to manage the state of a download. You could use strings like "loading", "success", "error". But what if you make a typo and write "succes"? The program would fail and you wouldn’t notice until runtime.

With Enums, the compiler protects us.

// Enum definition (usually outside main)
enum DownloadState { loading, completed, error }

void main() {
  // Using the Enum
  DownloadState currentState = DownloadState.loading;
  
  if (currentState == DownloadState.completed) {
    print("File ready");
  }
}
Copied!

switch and enums

The switch evaluates an expression and executes the code associated with the matching case.

In Dart, when you combine switch with enums, a very interesting feature appears: Exhaustiveness Checking.

When you use an enum in a switch, the analyzer can warn you if you do not cover all possible cases. If you add a new state to the enum, these diagnostics help you locate the switch statements you need to update.

DownloadState state = DownloadState.error;

switch (state) {
  case DownloadState.loading:
    print("Please wait...");
    break;
  case DownloadState.completed:
    print("Ready!");
    break;
  case DownloadState.error:
    print("Oops, something went wrong");
    break;
}
Copied!

If you remove one of the case statements, the analyzer will point out that the switch is not exhaustive. You can also add a default case, although doing so will lose the specific warning when a new enum value appears.

Loops for repeating tasks

To repeat blocks of code, Dart offers the classic tools, but with some modern optimizations.

for loop

The classic one. Useful when you need the numeric index.

for (int i = 0; i < 5; i++) {
  print("Loop number $i");
}
Copied!

for-in loop

This is the one we will use 90% of the time. It allows us to iterate over a list element by element without worrying about indices.

List<String> colors = ["Red", "Green", "Blue"];

for (var color in colors) {
  print("The color is $color");
}
Copied!

while and do-while

They execute while a condition is true.

  • while: Evaluates the condition before executing.
  • do-while: Executes at least once and then evaluates.
int counter = 0;

while (counter < 3) {
  print(counter);
  counter++;
}
Copied!

break and continue

Sometimes we need to alter the normal flow of a loop:

  • break: Completely stops the loop and exits it.
  • continue: Stops the current iteration and jumps to the next one.
for (int i = 0; i < 10; i++) {
  if (i == 5) break; // Terminates the loop when reaching 5
  print(i);
}
Copied!