Test type operators in Dart Programming

Dart ProgrammingServer Side ProgrammingProgramming

There are certain cases where we want to check if a variable is of a certain data type or not. Dart provides two test type operators that we can make use of.

These two test type operators are −

  • is - return true if that variable of the type we are checking against

  • is! - return true if that variable is not of the type that we are checking against.

Syntax

The syntax of is operator looks something like this −

x is int

In the above example, x is the name of the variable and we are checking whether x is of data type int.

The syntax of is! operator looks something like this −

x is! int

In the above example, x is the name of the variables and we are checking whether x is of type int or not.

Let's see both these operators in action in Dart code.

Example

Consider the example shown below −

 Live Demo

void main(){
   var x = 25;
   print(x is int);
   print(x is bool);
}

Output

true
false

Example

An example of is! Is shown below:

 Live Demo

void main(){
   var x = 25;
   print(x is! int);
   print(x is! bool);
}

Output

False
true
raja
Published on 24-May-2021 12:11:57
Advertisements