This episode masters Dart syntax foundations: variable declaration with var, final, and const, complete control flow, functions with optional parameters and lambdas, and the List, Set, and Map collections along with collection-for.

Now that your project runs, it's time to fill the code with real language. Episode 4 covers the syntax and basic language features you'll use every day: how to declare variables, control program flow, define functions, and work with collections.
Dart is designed to be readable and expressive. Many concepts here will feel familiar if you've written JavaScript or C#, but there are Dart-specific details that set it apart — like final versus const, named parameters, and collection-for.
By the end of this episode, you'll be able to write a complete small program: from reading input, processing data, to displaying results.
Declare variables with var and let the compiler infer their types:
void main() {
var nama = 'Dart';
var versi = 3;
var harga = 19.99;
print('$nama $versi $harga');
}nama is a String, versi is an int, and harga is a double — all inferred automatically. Once bound, the type cannot change.
Use final for values assigned only once, and const for compile-time constant values:
void main() {
final nama = 'Dart'; // assign sekali
const pi = 3.14; // konstanta kompilasi
print('$nama $pi');
}The key difference: const is used when the value is known before the program runs, while final is used when the value is only known at runtime but must not change afterward. Running dart run on this file still produces the same output because both cannot be reassigned.
if and switch control branching. Starting with Dart 3, switch can use patterns and expressions:
void main() {
var nilai = 85;
if (nilai >= 80) {
print('Lulus dengan predikat baik');
} else {
print('Perlu belajar lagi');
}
}Dart supports for, while, do-while, and for-in. There's also collection-for, which lets you build collections inside a collection literal:
void main() {
for (var i = 1; i <= 3; i++) {
print('Iterasi $i');
}
var angka = [1, 2, 3];
var ganda = [for (var a in angka) a * 2];
print(ganda);
}for (var a in angka) a * 2 produces the list [2, 4, 6] in a single expression — a very useful pattern for data transformation.
Dart distinguishes optional positional parameters, wrapped in square brackets, and optional named parameters, wrapped in curly braces. Named parameters must be marked required if they have no default:
void sapa(String nama, [String sapaan = 'Halo']) {
print('$sapaan, $nama!');
}
void profil({required String nama, int umur = 0}) {
print('$nama berumur $umur');
}
void main() {
sapa('Arman');
profil(nama: 'Arman', umur: 30);
}The call profil(nama: 'Arman') uses the default age of 0 because the umur parameter is optional and named. Note: when calling named parameters, order doesn't matter.
Functions are first-class objects in Dart. You can write anonymous functions and arrow functions with =>:
void main() {
var lipatGanda = (int x) => x * 2;
var daftar = [1, 2, 3];
var hasil = daftar.map(lipatGanda).toList();
print(hasil);
}daftar.map(lipatGanda) applies the function to each element and returns the results. The arrow function (int x) => x * 2 is shorthand for a block that directly returns an expression.
Dart's three main collections use similar literals:
void main() {
var list = [1, 2, 3];
var set = {1, 2, 2, 3}; // duplikat otomatis dibuang
var map = {'nama': 'Dart', 'versi': 3};
print(list[0]);
print(set);
print(map['nama']);
}A Set guarantees unique elements — the second 2 is automatically dropped. A Map is a key-value pair accessed with map['nama']. All three can be combined with collection-for and collection-if to build data declaratively.
Key takeaways:
var for type inference, final for single assignment, const for compile-time values.switch in Dart 3 supports patterns; use it according to your branching needs.required.=>.In the next episode 5, we'll move into object-oriented Dart — classes and constructors, inheritance and mixins, abstract classes and enums, extension methods and operator overloading, and building immutable data models. This is the foundation for writing structured Dart applications.