DEV Community

Cover image for Operators in Dart
Jay Tillu
Jay Tillu

Posted on • Edited on

Operators in Dart

An expression is a special kind of statement that evaluates in a single value.

Every expression is composed of two parts:

  1. Operand — They represents data.
  2. Operator — An operator is a symbol that tells the compiler to perform a specific mathematical, relational, or logical operation and produce a final result.
  • For example, A + B where
  • Here A and B are operands and + is an operator.

Types of Operators in Dart


  • There are total of eight types of operators are there in Dart.
  1. Arithmetic Operator
  2. Assignment Operator
  3. Bitwise operator
  4. Equality Operator
  5. Logical Operator
  6. Relational Operator
  7. Short circuit operator
  8. Type Test Operator

Arithmetic Operator


  • There are nine types of an arithmetic operator are there in a dart.

Relational Operator


  • Relational operator shows the relation between the two operands.
  • The relational operator can have only two values either true or false.
  • There are four relational operators are there.

Equality Operator


  • Equality operator checks whether two objects are equal or not.
  • Same as relational operator they contain Boolean value either true or false.
  • There are two equality operators are there.

  • Here remember that two strings are equivalent only if they contain the same character sequence. Here the sequence of character is important rather than the number of character.
void main() {
 var a = "Jay";
 var b = "Jya"; //If here value is Jay then this is true
 print(a == b);
}

Output
false
Enter fullscreen mode Exit fullscreen mode

Type Test Operator


  • Type test operators are used to check the data type.

Assignment Operator


  • Dart has main two assignment operator.

  • Here note that (+=, -=, *=, /=) are also a type of assignment operator. But as they are just a shortcut way. I didn’t mention them.

Logical Operator


  • Logical operators are used to combine two or more conditions.
  • Logical operators return a Boolean value of true or false.
  • There are three logical operators are there:

Conditional Expression


  • Dart has two special operators which can help you to evaluate small expressions that might require an entire if-else statement.

Condition? expr1: expr2


  • Here if the condition is true then the expr1 is evaluated and returns its value. Otherwise, expr2 is evaluated and returns the value.
void main() {
  var a = 25;
  var res = a > 15 ? "value greater than 15" : "value lesser than or equal to 15";
  print(res);
}

Output
value greater than 15
Enter fullscreen mode Exit fullscreen mode

expr1 ?? expr2


  • Here if expr1 is non-null, return its value. Otherwise, evaluate and return the value of expr2.
void main() {
  var a = 15;
  var b = null;
  var res = a ?? b;
  print ( res );
}

Output
15
Enter fullscreen mode Exit fullscreen mode

So, guys, that’s all you need to know about dart operators. Here short circuit and bitwise operators are also there. But as they are rarely used so I didn’t mention them here. But feel free to explore them if you need one. Also, feel free to let me know if I missed something. I’ll love to learn it from you.

Till Then Keep Loving, Keep Coding. And I’ll surely catch you up in another amazing article.

Remember no teacher, no book, no video tutorial, or no blog can teach you everything. As one said Learning is Journey and Journey never ends. Just collect some data from here and there, read it, learn it, practice it, and try to apply it. Don’t feel hesitate that you can’t do that or you don’t know this concept or that concept. Remember every programmer was passed from the path on which you are walking right now. Remember Every Master was Once a Beginner. Work hard and Give your best.

Learn more about Dart and Flutter

Follow me for more such content

Top comments (0)