Dart Fundamentals
What is Dart?
Dart is the programming language Flutter uses, created by Google. It’s easy to pick up if you know any C-style language (Java, JavaScript, Swift, Kotlin). It’s modern, type-safe, and null-safe, and it compiles to fast native code for release builds.
Variables and types
var name = 'Anand'; // type inferred as String
String city = 'Delhi'; // explicit type
final pi = 3.14; // set once, can't change
const max = 100; // compile-time constant
int age = 25;
double price = 99.5;
bool isActive = true;
final and const both mean “can’t be reassigned”; the difference is const must be known at compile time. Prefer them over var when a value won’t change.
Null safety
Like Kotlin and Swift, Dart is null-safe: a variable can’t be null unless you add a ?. This stops a huge class of crashes.
String name = 'Anand'; // can never be null
String? nickname; // may be null
print(nickname ?? 'Guest'); // ?? provides a default
print(nickname?.length); // ?. safe access
Functions
int add(int a, int b) => a + b; // arrow for one-liners
String greet(String name, {String greeting = 'Hi'}) { // named param + default
return '$greeting, $name!';
}
greet('Anand'); // Hi, Anand!
greet('Priya', greeting: 'Hello');
The { } makes parameters named — Flutter uses these everywhere (you’ll see Text('hi', style: ...)), which makes widget constructors very readable.
Collections
final nums = [1, 2, 3, 4]; // List
final ages = {'Anand': 28, 'Priya': 34}; // Map
final unique = {1, 1, 2}; // Set -> {1, 2}
final doubled = nums.map((n) => n * 2).toList(); // [2,4,6,8]
final evens = nums.where((n) => n.isEven).toList(); // [2,4]
final total = nums.fold(0, (sum, n) => sum + n); // 10
Classes
class User {
final int id;
final String name;
User(this.id, this.name); // shorthand constructor
String describe() => 'User #$id: $name';
}
final u = User(1, 'Anand');
print(u.describe());
Async with Future and async/await
Future<String> loadData() async {
await Future.delayed(Duration(seconds: 1)); // pretend network call
return 'Done';
}
void main() async {
final result = await loadData(); // waits without blocking
print(result);
}
Common mistakes
- Using
vareverywhere instead offinal/constfor fixed values. - Forgetting
.toList()aftermap/where(they return lazy iterables). - Calling an
asyncfunction withoutawaitand wondering why the result is aFuture.
Summary: Dart is a modern, null-safe language. Usefinal/const, handle nullables with?/??, lean on named parameters (Flutter loves them), and useasync/awaitwithFuturefor asynchronous work.