Keywords are reserved words in the Dart programming language that have a special predefined meaning. These words are part of Dart’s syntax and cannot be used as variable names, function names, or class names.
Understanding keywords is essential because they form the grammar of Dart. Every Dart and Flutter program is built using these keywords.
Why Keywords Matter
Keywords:
-
Define program structure
-
Control program flow
-
Declare variables, functions, and classes
-
Enable object-oriented and asynchronous programming
In Flutter, keywords are constantly used for:
-
Widget creation
-
State management
-
Async API calls
-
App navigation and logic
Categories of Keywords in Dart
For learning purposes, Dart keywords can be grouped into logical categories.
1. Variable Declaration Keywords
These keywords are used to declare variables.
var
Infers the variable type automatically.
void main() {
var name = 'Rabin';
print(name);
}
final
Used when a variable should be assigned only once.
void main() {
final int year = 2025;
print(year);
}
const
Used for compile-time constants.
void main() {
const double pi = 3.14159;
print(pi);
}
2. Control Flow Keywords
These keywords control the execution flow of a program.
if, else
void main() {
int marks = 60;
if (marks >= 40) {
print('Passed');
} else {
print('Failed');
}
}
switch, case, default
void main() {
int day = 3;
switch (day) {
case 1:
print('Sunday');
break;
case 2:
print('Monday');
break;
default:
print('Other day');
}
}
Loop Keywords: for, while, do, break, continue
void main() {
for (int i = 1; i <= 5; i++) {
if (i == 3) continue;
print(i);
}
}
3. Function-Related Keywords
void
Used when a function does not return a value.
void greet() {
print('Hello Dart');
}
return
Used to return a value from a function.
int add(int a, int b) {
return a + b;
}
4. Object-Oriented Programming Keywords
Dart is an object-oriented language.
class
class Student {
String name;
int age;
Student(this.name, this.age);
}
extends
Used for inheritance.
class Person {
void speak() {
print('Speaking');
}
}
class Student extends Person {}
this
Refers to the current object.
class User {
String name;
User(this.name);
}
5. Asynchronous Programming Keywords
These keywords are heavily used in Flutter for API calls and async operations.
async and await
Future<void> fetchData() async {
await Future.delayed(Duration(seconds: 2));
print('Data loaded');
}
Future
Represents a value that will be available later.
Future<int> getNumber() async {
return 10;
}
6. Null Safety Keywords
null
Represents absence of value.
String? nickname = null;
late
Used when a variable will be initialized later.
late String token;
void main() {
token = 'abc123';
print(token);
}
Common Beginner Mistakes
-
Using keywords as variable names
-
Confusing
finalandconst -
Forgetting
breakin switch cases -
Misusing
lateanddynamic
Summary
In this lesson, you learned:
-
What keywords are
-
Major categories of Dart keywords
-
How keywords control program structure and behavior
A strong understanding of keywords will make reading and writing Dart code much easier.
Assignment
Task 1:
Declare one variable using var, one using final, and one using const. Print all three.
Task 2:
Write a program that checks whether a number is positive, negative, or zero using if and else.
Task 3:
Use a for loop to print numbers from 1 to 10, but skip number 5 using continue.
Task 4:
Create a simple function that returns the square of a number using return.
Task 5 (Thinking):
Explain in your own words when you would use final instead of var in a Flutter app.