Dart Miscellaneous Operators
Apart from the operators we studied so far, Dart provides some more operators which are as follows :
- Conditional Operator
- Cascade Notations
Conditional Operators
These are the operators that help in evaluating the expression that otherwise would have required if-else statements.
Syntax:
Condition ? expr1 : expr2
Condition here refers to a test expression that may evaluate to true or false. If it evaluates to true then expr1 is executed and if it evaluates to false, then expr2 is executed.
Program
Consider the following code in Dart that explains Conditional Operator
import 'dart:io';
void main( ) {
print( ' Enter your age : ' ) ;
int? age = int.parse( stdin.readLineSync ( ) ! ) ;
var res = age >= 18
? " You are eligible to vote. "
: " You are not eligible to vote. " ;
print( res ) ;
}
Output:
Enter your age :
20
You are eligible to vote.
Dart Cascade Operators
The Cascade notation Operators (..) is used to evaluate a series of operation on the same object. It is an identical as the method chaining that avoids several of steps, and we don't need store results in temporary variables.