What are the keywords in Dart programming?

Dart keywords are reserved words or terms in the Dart programming language that the compiler interprets differently.

These keywords can’t be used as the name of a variable, class, or function.

Dart keywords are case sensitive and must be defined properly.

The Dart programming language has a total of 61 keywords.

Dart keywords

abstract

else

import

super

as

enum

in

switch

assert

export

interface

sync

async

extend

is

this

await

extension

library

throw

break

external

mixin

true

case

factory

new

try

class

final

catch

false

null

typedef

on

var

const

finally

operator

void

continue

for

part

while

covariant     

function

rethrow

with

default

get

return

yield

deferred

hide

set

do

if

show

dynamic

implements

static




Examples

1. catch

catch is used with the try block to catch exceptions in the program.

Syntax:-
try {  
  // program that might throw an exception  
}  
catch Exception1 {  
  // code for handling exception 1
}

2. finally

finally is a block of codes that should be run even if the program encounters an exception. The finally block comes after the try block, and anytime an error occurs, control is passed to the finally block, which is then executed.

3. try

In Dart, the try block is used to keep those codes that can cause a program to crash. A try block is followed by at least one on/catch block or a finally block.

//using try, catch, var, finally keywords
void main() {
int num = 20;
try{
var num2 = num ~/ 0;
print(num2);
}
catch(e){
print(e);
}
finally {
print("End of program");
}
}

4. in

in is used in a for in loop to iterate over an iterable (such as a list or set).

void main() {
var myList = [23, 42, 13, 15, 90];
for (var item in myList)
print(item);
}

5. switch

The switch keyword is used in the switch case statement, which is a simplified version of the nested if-else statement.

6. default

In a switch case, if no condition matches, the default statement is executed.

7. `case

case is used with a switch statement.

8. break

break is used to end the loop and switch case when the condition is met.

//using keyword switch,case,default,break
void main()
{
int num = 2;
switch (num) {
case 1: {
print("Shot 1");
} break;
case 2: {
print("Shot 2");
} break;
case 3: {
print("Shot 3");
} break;
default: {
print("This is default case");
} break;
}
}