Type Test Operators
This class of operators contain those operators which are used to perform comparison on the operands. It goes like this:
Operator Symbol | Operator Name | Operator Description |
---|---|---|
is | is | Gives boolean value true as output if the object has specific type |
is! | is not | Gives boolean value false as output if the object has specific type |
Example: Using Type Test Operators in the program
void main()
{
String a = 'GFG';
double b = 3.3;
// Using is to compare
print(a is String);
// Using is! to compare
print(b is !int);
}
Output:
true
true
as Operator
as Operator is used for Typecasting. It performs a cast at runtime if the cast is valid else, it throws an error. It is of two types Downcasting and Type Assertion.
Below is the implementation of as operator:
// Dart Program to demonstrate
// Use of as Operator
void main(){
// Declaring value
dynamic value = "Hello";
// TypeCast dynamic -> String
String str= value as String;
// Print String
print(str);
}
Output:
Hello
Operators in Dart
The operators are special symbols that are used to carry out certain operations on the operands. The Dart has numerous built-in operators which can be used to carry out different functions, for example, ‘+’ is used to add two operands. Operators are meant to carry operations on one or two operands.
Contact Us