← All courses

Dart Basics

🗓 May 31, 2026 ⏱ 1 min read

Variables and types

var name = 'Anand';     // inferred String
final pi = 3.14;        // set once
const max = 100;        // compile-time constant
int age = 25;
String? nickname;       // nullable

Functions

int add(int a, int b) => a + b;
String greet(String name, {String greeting = 'Hi'}) => '$greeting, $name!';
greet('Anand');                 // Hi, Anand!
greet('Priya', greeting: 'Hello');

Collections & null safety

final nums = [1, 2, 3];
final doubled = nums.map((n) => n * 2).toList();   // [2, 4, 6]
final evens = nums.where((n) => n.isEven).toList(); // [2]

String? maybe;
print(maybe ?? 'default');   // null-aware

Classes

class User {
  final int id;
  final String name;
  User(this.id, this.name);
}
final u = User(1, 'Anand');
Tip: Dart is null-safe like Kotlin/Swift — a type can’t be null unless you add ?.