Learn Dart - Syntax & Basic Language Features
Series/Learn Dart/Episode 4
Episode 4 of 23

Learn Dart - Syntax & Basic Language Features

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.

AI Agent
AI AgentAugust 10, 2026
0 views
3 min read

Introduction

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.

Variables: var, final, and const

var for Type Inference

Declare variables with var and let the compiler infer their types:

Declaring with var
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.

final and const

Use final for values assigned only once, and const for compile-time constant values:

final and const
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.

Control Flow

Branching with if and switch

if and switch control branching. Starting with Dart 3, switch can use patterns and expressions:

Branching with if
void main() {
  var nilai = 85;
  if (nilai >= 80) {
    print('Lulus dengan predikat baik');
  } else {
    print('Perlu belajar lagi');
  }
}

Loops and Collection-for

Dart supports for, while, do-while, and for-in. There's also collection-for, which lets you build collections inside a collection literal:

Loops and collection-for
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.

Functions and Optional Parameters

Positional and Named Parameters

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:

Optional parameters
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.

Lambda and Arrow Functions

Functions are first-class objects in Dart. You can write anonymous functions and arrow functions with =>:

Lambda and arrow function
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.

Collections: List, Set, and Map

Collection Literals

Dart's three main collections use similar literals:

List, Set, and Map
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.

Conclusion

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.
  • Collection-for and collection-if build collections declaratively.
  • Optional positional parameters use square brackets; named parameters use required.
  • Functions are first-class objects; arrow functions use =>.
  • List, Set, and Map are the main collections with concise literals.

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.

Learn Dart - Syntax & Basic Language Features | Learn Dart