Variables in Dart
Dart is the language Flutter uses, and it's surprisingly friendly. If you know JavaScript or Java, Dart will feel like home. Let's start with the basics — variables. Dart gives you four ways to declare variables: var, final, const, and late.
Use var when the type is inferred and the value might change. Use final when you assign once and never change it. Use const for compile-time constants — values that never change and are known at compile time. And late? That's for when you'll initialize a variable later but still want it to be non-nullable.
void main() {
var name = 'Alice'; // Type inferred as String
final age = 30; // Can't reassign after assignment
const pi = 3.14159; // Compile-time constant
late String description; // Initialize later
description = 'A late variable';
print('$name is $age years old');
print('Pi is $pi');
print(description);
}
Try it Yourself →
Data Types
Dart has all the data types you'd expect: strings, numbers, booleans, lists, and maps. Strings use single or double quotes — both work. Numbers include int for whole numbers and double for decimals. Booleans are true or false, simple as that.
Lists are Dart's version of arrays. They're ordered collections that can hold any type. Maps are key-value pairs, like dictionaries in Python or objects in JavaScript.
void main() {
// Lists
var fruits = ['apple', 'banana', 'cherry'];
fruits.add('date');
print(fruits); // [apple, banana, cherry, date]
// Maps
var user = {
'name': 'Alice',
'age': 30,
'isDeveloper': true
};
print(user['name']); // Alice
// Nested collections
var matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
];
print(matrix[1][2]); // 6
}
Try it Yourself →
Null Safety
Dart is null-safe by default, which is a huge deal. Variables can't be null unless you explicitly say so with a question mark. This catches bugs at compile time instead of letting them crash your app at runtime. No more null pointer exceptions!
void main() {
String name = 'Alice';
// name = null; // This would cause a compile error!
String? nullableName; // This CAN be null
nullableName = 'Bob';
// Null-aware operators
String? unknownName;
print(unknownName ?? 'Unknown'); // Prints 'Unknown'
// Null check
if (unknownName != null) {
print(unknownName.length);
}
}
The ?? operator is called the null-coalescing operator. It returns the left side if it's not null, otherwise it returns the right side. Super handy for providing default values.
Control Flow
Dart's control flow will look familiar if you've used any C-style language. if/else works exactly as you'd expect. For loops, while loops, switch statements — they're all here and they all work the same way.
void main() {
var score = 85;
// if/else
if (score >= 90) {
print('Grade: A');
} else if (score >= 80) {
print('Grade: B');
} else {
print('Grade: C');
}
// for loop
for (var i = 0; i < 5; i++) {
print('Count: $i');
}
// for-in loop
var colors = ['red', 'green', 'blue'];
for (var color in colors) {
print(color);
}
// switch
var day = 'Monday';
switch (day) {
case 'Monday':
print('Start of the week');
break;
case 'Friday':
print('Almost weekend!');
break;
default:
print('Just another day');
}
}
Try it Yourself →