Keywords in Dart
Keywords
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.
List of Keywords
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. |
print | 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
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. If you want your abstract class to appear to be instantiable, define a factory constructor.
abstract class AbstractContainer {
// Define constructors, fields, methods...
void updateChildren(); // Abstract method.
}
class SpecializedContainer extends AbstractContainer {
void updateChildren() {
// Provide an implementation, so the method is not abstract here...
}
}
If you didn't understand the code as of now, don't worry. We will learn about classes in the upcoming lessons.
static
static
keyword is used to create class-level variables and methods. Static variables and methods are shared by all instances of a class. Static members are accessed using the class name as a qualifier.
class Queue {
static const initialCapacity = 16;
// ···
}
void main() {
assert(Queue.initialCapacity == 16);
}
const
const
keyword is used to create compile-time constants. Compile-time constants are constants whose values will be determined at compile time. If the const variable is at the class level, mark it static const. Where you declare the variable, set the value to a compile-time constant such as a number or string literal, a const variable, or the result of an arithmetic operation on constant numbers.
class ImmutablePoint {
static const ImmutablePoint origin = const ImmutablePoint(0, 0);
final double x, y;
const ImmutablePoint(this.x, this.y);
}
final
final
keyword is used to create variables that can be set only once. Dart supports two types of final variables: those that are initialized when they’re declared and those that are initialized the first time they’re used. Use const for variables that you want to be compile-time constants. Final instance variables must be initialized before the constructor body starts — at the variable declaration, by a constructor parameter, or in the constructor’s initializer list.
class Point {
final num x;
final num y;
final num distanceFromOrigin;
Point(x, y)
: x = x,
y = y,
distanceFromOrigin = sqrt(x * x + y * y);
}
const vs final
Although both of the keywords are used to create constants, there is a difference between them. Let's understand the difference between them.
const
const
keyword is used to create compile-time constants. Compile-time constants are constants whose values will be determined at compile time. If the const variable is at the class level, mark it static const. Where you declare the variable, set the value to a compile-time constant such as a number or string literal, a const variable, or the result of an arithmetic operation on constant numbers.
class ImmutablePoint {
static const ImmutablePoint origin = const ImmutablePoint(0, 0);
final double x, y;
const ImmutablePoint(this.x, this.y);
}
final
final
keyword is used to create variables that can be set only once. Dart supports two types of final variables: those that are initialized when they’re declared and those that are initialized the first time they’re used. Use const for variables that you want to be compile-time constants. Final instance variables must be initialized before the constructor body starts — at the variable declaration, by a constructor parameter, or in the constructor’s initializer list.
class Point {
final num x;
final num y;
final num distanceFromOrigin;
Point(x, y)
: x = x,
y = y,
distanceFromOrigin = sqrt(x * x + y * y);
}
Conclusion
These are the few keywords that we have learned in this lesson. We will learn more about them in the upcoming lessons.