
Flutter Basics: Variables and Loops (Quick Reference)
If you’re just starting with Flutter or need a quick refresher on Dart fundamentals, this post covers the essentials of variables and loops. It’s designed for fast reference, without unnecessary extras.
Flutter apps are built using Dart, a modern, object-oriented language developed by Google. Understanding Dart basics is essential for writing effective Flutter code.
Variables store data. Dart supports both explicitly typed and type-inferred variable declarations.
int age = 25; // Integer
double height = 5.9; // Decimal
String name = "Kaustav"; // Text
bool isActive = true; // Boolean
var city = "Delhi"; // Type inferred as String
final country = "India"; // Value set once, at runtime
const pi = 3.14; // Compile-time constant
var
when the variable’s type can be inferred and might change.final
for values that are assigned once and do not change.const
for values that are constant at compile time.Loops are used to execute a block of code multiple times.
for (int i = 0; i < 5; i++) {
print("Count: $i");
}
Ideal for iterating over collections like lists.
List fruits = ["Apple", "Banana", "Mango"];
for (var fruit in fruits) {
print(fruit);
}
int i = 0;
while (i < 3) {
print("i is $i");
i++;
}
int j = 0;
do {
print("j is $j");
j++;
} while (j < 3);
Use string interpolation to embed variables inside strings.
String name = "Flutter";
print("Hello, $name"); // Output: Hello, Flutter
Concept | Example |
---|---|
Variable | int count = 0; |
Inferred Type | var name = "Dart"; |
Final | final version = "1.0"; |
Const | const pi = 3.14; |
For Loop | for (int i = 0; i < 3; i++) |
For-in Loop | for (var item in list) |
While Loop | while (condition) |
Do-While Loop | do { } while (condition) |
To practice Dart code without setting up a project, visit https://dartpad.dev.
If you’d like a follow-up guide on conditionals, functions, or classes, let me know.
0 Comments
Be the first to comment