Last Updated: November 21, 2025
Dart
Dart programming language essentials
Basic Syntax
// Variables
var name = 'John';
String greeting = 'Hello';
int age = 30;
double price = 19.99;
bool isActive = true;
// Null safety
String? nullableName;
String nonNullName = nullableName ?? 'Default';
// Functions
int add(int a, int b) => a + b;
void greet(String name, {int age = 0}) {
print('Hello $name, age $age');
}
// Lists and Maps
List<String> fruits = ['apple', 'banana'];
Map<String, int> scores = {'Alice': 100, 'Bob': 85};
// Classes
class Person {
String name;
int age;
Person(this.name, this.age);
void introduce() {
print('I am $name, $age years old');
}
}
Collections
// Lists
var list = [1, 2, 3];
list.add(4);
list.remove(2);
list.forEach((item) => print(item));
// Sets
var uniqueNumbers = {1, 2, 3};
uniqueNumbers.add(1); // No duplicate
// Maps
var person = {'name': 'John', 'age': 30};
person['email'] = 'john@example.com';
// Spread operator
var combined = [...list, ...uniqueNumbers];
// Collection if
var nav = ['Home', if (isLoggedIn) 'Logout'];
Async/Await
Future<String> fetchData() async {
await Future.delayed(Duration(seconds: 2));
return 'Data loaded';
}
void main() async {
print('Loading...');
var data = await fetchData();
print(data);
}
// Streams
Stream<int> countStream() async* {
for (int i = 1; i <= 5; i++) {
await Future.delayed(Duration(seconds: 1));
yield i;
}
}
void listenToStream() async {
await for (var value in countStream()) {
print(value);
}
}
Useful Features
- Strong typing with type inference
- Null safety by default
- Named and optional parameters
- Extension methods
- Mixins for code reuse
- Cascade notation (..) for chaining
- Collection literals with type inference
💡 Pro Tips
Quick Reference
Use final for variables that won't be reassigned