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

List of Keywords

KeywordDescription
abstractUsed to declare a class as abstract.
asUsed to specify a library prefix or as a type cast.
assertUsed to assert that a condition is true.
asyncUsed to specify that a function, method or block of code is asynchronous.
awaitUsed to suspend execution until an asynchronous operation completes.
breakUsed to terminate the execution of a loop, switch statement or labeled statement.
caseUsed to define a specific condition in a switch statement.
catchUsed to specify a block of code to be executed, if an error occurs in the try block.
classUsed to declare a class.
constUsed to declare a variable as a compile-time constant.
continueUsed to continue the execution of a loop, switch statement or labeled statement.
defaultUsed to specify the default block of code in a switch statement.
deferredUsed to load a library only when it is needed.
doUsed to declare a loop that will iterate based on a condition.
dynamicUsed to declare a variable without specifying a type.
elseUsed to specify a block of code to be executed, if the same condition is false.
enumUsed to declare an enumerated (unchangeable) type.
exportUsed to export a library.
extendsUsed to specify a parent class.
extensionUsed to declare an extension method.
externalUsed to declare a function that is implemented externally.
factoryUsed to declare a factory method.
falseUsed to represent a Boolean false value.
finalUsed to declare a variable as final (read-only).
finallyUsed to specify a block of code to be executed, after a try/catch block, regardless of the result.
forUsed to declare a loop that will iterate based on a condition.
FunctionUsed to specify a function type.
getUsed to declare a getter method or property.
printUsed to print a message to the console.
throwUsed to throw expcetions in a try/catch block.
trueUsed to represent a Boolean true value.
tryUsed to specify a block of code to be tested for errors.
typedefUsed to declare a function type alias.
varUsed to declare a variable.
voidUsed to declare a function that returns no value.
whileUsed to declare a loop that will iterate based on a condition.
withUsed to specify a mixin.
yieldUsed to pause and resume a generator function.
yield*Used to delegate to another generator function.
importUsed to import a library.
inUsed to specify the iterable element in a for loop.
isUsed to test if an object is a specific type.
libraryUsed to specify a library name that will be used in the import statements.
futureUsed to specify a future type.
streamUsed 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 in dart

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!
  

code Try Yourself

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.

Quiz

Test your understanding!