Dart Keywords
Reserved words that have special meaning in Dart
🔑 What are Dart Keywords?
Dart keywords are reserved words with special meanings in the language. They cannot be used as identifiers and control program flow, define types, and structure code.
// Keywords in action
class MyClass {
final String name;
static const int version = 1;
MyClass(this.name);
}
Keywords used:
class - defines a class
final - creates immutable variable
static - class-level member
const - compile-time constant
Keyword Categories
Declaration
Define variables and functions
var name = 'John';
final age = 25;
const pi = 3.14;
Control Flow
Control program execution
if (age > 18) {
return 'Adult';
} else {
return 'Minor';
}
Object-Oriented
Define classes and inheritance
class Animal extends Object {
void speak() => print('Sound');
}
Async
Handle asynchronous operations
Future fetchData() async {
await Future.delayed(Duration(seconds: 1));
return 'Data loaded';
}
🔹 Variable Declaration Keywords
Keywords for declaring variables with different properties:
// var - type inferred
var message = 'Hello World';
// final - runtime constant (immutable)
final DateTime now = DateTime.now();
// const - compile-time constant
const String appName = 'My App';
// late - lazy initialization
late String expensiveValue;
Usage:
var: Type determined automatically
final: Set once, cannot change
const: Known at compile time
late: Initialize when first used
🔹 Control Flow Keywords
Keywords that control how your program executes:
🔸 Conditional Keywords
// if, else, else if
int score = 85;
if (score >= 90) {
print('A grade');
} else if (score >= 80) {
print('B grade');
} else {
print('Try harder');
}
// switch, case, default
switch (score ~/ 10) {
case 10:
case 9:
print('Excellent');
break;
case 8:
print('Good');
break;
default:
print('Keep studying');
}
🔸 Loop Keywords
// for loop
for (int i = 0; i < 5; i++) {
print('Count: $i');
}
// while loop
int count = 0;
while (count < 3) {
print('While: $count');
count++;
}
// for-in loop
List fruits = ['apple', 'banana', 'orange'];
for (String fruit in fruits) {
print('Fruit: $fruit');
}
🔹 Class and Object Keywords
Keywords for object-oriented programming:
// abstract class
abstract class Shape {
void draw(); // abstract method
}
// class with inheritance
class Circle extends Shape {
double radius;
Circle(this.radius);
@override
void draw() {
print('Drawing circle with radius $radius');
}
}
// interface implementation
class Square implements Shape {
double side;
Square(this.side);
@override
void draw() {
print('Drawing square with side $side');
}
}
🔹 Async Keywords
Keywords for handling asynchronous operations:
// async function returns Future
Future loadUserData() async {
// await pauses execution until Future completes
String userData = await fetchFromServer();
return userData;
}
// Using async/await
void main() async {
try {
String data = await loadUserData();
print('User data: $data');
} catch (e) {
print('Error: $e');
}
}
Async Flow:
1. Function marked with async
2. await pauses until operation completes
3. Execution continues with result
🔹 Complete Keywords List
All Dart Keywords:
abstract - abstract class
as - type cast
assert - debug assertion
async - async function
await - wait for Future
break - exit loop
case - switch case
catch - exception handling
class - define class
const - compile constant
continue - next iteration
default - default case
do - do-while loop
else - alternative condition
enum - enumeration
extends - inheritance
false - boolean false
final - runtime constant
finally - cleanup code
for - for loop
if - condition
implements - interface
import - import library
in - for-in loop
is - type check
late - lazy initialization
new - create instance
null - null value
return - return value
static - class member
super - parent class
switch - switch statement
this - current instance
throw - throw exception
true - boolean true
try - exception handling
var - variable declaration
void - no return value
while - while loop
with - mixin