Keywords in Dart
Keywords are reserved words that have a specific meaning to the compiler and cannot be used as identifiers (names for variables, functions, classes, etc.).
Examples of keywords include class, const, for, if, void, and while. We we understand them fully once we use them in our code in the upcomming lessons.
Familiar with the topic? Play Quiz
Keyword | Description |
---|---|
abstract | Used to declare a class as abstract. |
as | Used to specify a library prefix or as a type cast. |
assert | Used to assert that a condition is true. |
async | Used to specify that a function, method or block of code is asynchronous. |
await | Used to suspend execution until an asynchronous operation completes. |
break | Used to terminate the execution of a loop, switch statement or labeled statement. |
case | Used to define a specific condition in a switch statement. |
catch | Used to specify a block of code to be executed, if an error occurs in the try block. |
class | Used to declare a class. |
const | Used to declare a variable as a compile-time constant. |
continue | Used to continue the execution of a loop, switch statement or labeled statement. |
default | Used to specify the default block of code in a switch statement. |
deferred | Used to load a library only when it is needed. |
do | Used to declare a loop that will iterate based on a condition. |
dynamic | Used to declare a variable without specifying a type. |
else | Used to specify a block of code to be executed, if the same condition is false. |
enum | Used to declare an enumerated (unchangeable) type. |
export | Used to export a library. |
extends | Used to specify a parent class. |
extension | Used to declare an extension method. |
external | Used to declare a function that is implemented externally. |
factory | Used to declare a factory method. |
false | Used to represent a Boolean false value. |
final | Used to declare a variable as final (read-only). |
finally | Used to specify a block of code to be executed, after a try/catch block, regardless of the result. |
for | Used to declare a loop that will iterate based on a condition. |
Function | Used to specify a function type. |
get | Used to declare a getter method or property. |
Used to print a message to the console. | |
throw | Used to throw expcetions in a try/catch block. |
true | Used to represent a Boolean true value. |
try | Used to specify a block of code to be tested for errors. |
typedef | Used to declare a function type alias. |
var | Used to declare a variable. |
void | Used to declare a function that returns no value. |
while | Used to declare a loop that will iterate based on a condition. |
with | Used to specify a mixin. |
yield | Used to pause and resume a generator function. |
yield* | Used to delegate to another generator function. |
import | Used to import a library. |
in | Used to specify the iterable element in a for loop. |
is | Used to test if an object is a specific type. |
library | Used to specify a library name that will be used in the import statements. |
future | Used to specify a future type. |
stream | Used to specify a stream type. |
These are the only few keywords but thre are more to explore. We will learn those slowly and understand them fully. Let’s dive a little deeper on some of the keywords.
asbtract keyword is used to declare a class as abstract. Abstract classes cannot be instantiated, but they can be subclassed. Abstract classes are useful for defining interfaces, often with some implementation.
Example
abstract class Vehicle {
void start(); // Abstract method
void stop(); // Abstract method
}
class Car extends Vehicle {
@override
void start() => print("Car started!");
@override
void stop() => print("Car stopped!");
}
class Bike extends Vehicle {
@override
void start() => print("Bike started!");
@override
void stop() => print("Bike stopped!");
}
void main() {
/// Accessing the class
Car().start();
Car().stop();
Bike().start() ;
Car().stop() ;
}
Output
Car started!
Car stopped!
Bike started!
Car stopped!
If you didn’t understand the code as of now, don’t worry. We will learn about classes in the upcoming lessons.You will understand the keywords use as we progress forward.
Test your understanding!