Dart Basics for Flutter
The essential Dart language features — variables, functions, and classes — you need for Flutter.
The language behind your apps
Before building UIs, you need the basics of Dart — the language Flutter uses. We'll cover exactly what you need to be productive: variables, functions, control flow, and classes. If you've programmed before, this will be quick and familiar; Dart's design is clean and conventional. Let's build the foundation efficiently.
Variables and types
Dart is typed, but flexible. You can declare a variable's type explicitly, or use var and let Dart figure it out. For values that won't change, use final (set once at runtime) or const (a compile-time constant):
String name = "Mehul"; // explicit type
int age = 19;
double price = 49.99;
bool isStudent = true;
var city = "Ahmedabad"; // Dart infers this is a String
final pi = 3.14159; // won't change once set
// String interpolation with $ — clean and readable
print("Hi $name, you are $age years old.");
print("Next year: ${age + 1}"); // use {} for expressions
The core types are what you'd expect: String for text, int and double for numbers, bool for true/false. Note Dart's lovely string interpolation with $ — drop variables right into strings, using ${...} for expressions. You'll use this constantly.
Lists and maps
For collections, Dart gives you lists (ordered collections, like arrays) and maps (key-value pairs, like dictionaries) — both essential for app data:
// A list of items
List<String> fruits = ["apple", "banana", "mango"];
fruits.add("orange");
print(fruits[0]); // apple
print(fruits.length); // 4
// A map of key-value pairs
Map<String, int> ages = {
"Mehul": 19,
"Riya": 20,
};
print(ages["Mehul"]); // 19
// Loop over a list
for (var fruit in fruits) {
print(fruit);
}
Lists and maps are the backbone of app data — a list of products to display, a map of settings. The List<String> syntax specifies what type the list holds (here, Strings), keeping your data type-safe. These work just like collections in other languages you may know.
Functions
Functions in Dart declare a return type, a name, and typed parameters. Dart also has a concise arrow syntax for single-expression functions — which you'll see everywhere in Flutter:
// A standard function
int add(int a, int b) {
return a + b;
}
// Arrow syntax for single-expression functions
int square(int x) => x * x;
// Named, optional parameters (common in Flutter) — note the {}
String greet({String name = "friend"}) {
return "Hello, $name!";
}
void main() {
print(add(3, 4)); // 7
print(square(5)); // 25
print(greet(name: "Mehul")); // Hello, Mehul!
print(greet()); // Hello, friend! (uses default)
}
Pay special attention to named parameters (the ones in curly braces, called like greet(name: "Mehul")) — Flutter uses these heavily when building widgets, so getting comfortable with them now will pay off immediately. They make code readable by labelling each argument.
Control flow
Dart's if/else, loops, and conditions work just like most languages — a quick refresher with the ternary operator that's handy in Flutter UIs:
int marks = 72;
if (marks >= 75) {
print("Distinction");
} else if (marks >= 60) {
print("Pass");
} else {
print("Try again");
}
// The ternary — used a LOT in Flutter to choose between two things
String result = marks >= 60 ? "Pass" : "Fail";
// Loops
for (int i = 1; i <= 3; i++) {
print("Count: $i");
}
The ternary operator (condition ? a : b) is especially useful in Flutter, where you'll often pick between two UI options based on a condition — like showing different text or colours depending on state.
Classes: the heart of Dart and Flutter
This is the most important part, because everything in Flutter is built with classes — every widget is a class. A class bundles data (fields) and behaviour (methods), with a constructor to set up new objects:
class Product {
String name;
double price;
// Constructor — the 'this.' shorthand sets the fields directly
Product(this.name, this.price);
// A method
String describe() {
return "$name costs Rs $price";
}
}
void main() {
var laptop = Product("Laptop", 50000);
print(laptop.name); // Laptop
print(laptop.describe()); // Laptop costs Rs 50000
}
Notice Dart's neat constructor shorthand: Product(this.name, this.price) automatically assigns the parameters to the fields — much cleaner than writing it out. Classes also support inheritance with extends (a class building on another), which matters enormously in Flutter: your screens and components will extend Flutter's widget classes. If you've done our Java, C++, or JavaScript courses, this OOP is familiar territory. You now have all the Dart you need. Next, we dive into Flutter's defining concept: widgets.
Finished "Dart Basics for Flutter"?
Mark this chapter complete so you can pick up exactly where you left off. Your progress saves locally — sign in to sync across devices.
Was this chapter clear?
