Control flows and looping
Before we can finish exploring the main
method in DartPad, we need to know how to control the flow of code execution. This is done through a series of control flow statements. These are very similar to other programming languages, so let's see what they look like in Dart.
If/else
Dart supports the standard if
, else if
, else
decision structure. It also supports if
statements without curly brackets, which are especially useful during Flutter widget definitions. In these if
statements, the next expression is evaluated if the condition is true
. You can see an example of this in the following code snippet:
String test = "test2"; if (test == "test1") { print("Test1"); } else if (test == "test2") { print("Test2"); } else { print("Something else"); } // Prints Test2 If (test == "test2") print("Test2 again"); // Prints Test 2...