🛠 Flutter world will be ready by the end of September, 2024 🚀🔥
Learn Dart Programming
Dart Basics
Keywords in Dart

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

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

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.