Dart Operators

In this tutorial you will learn about the Dart Operators and its application with practical example.

Dart Operators

An operator is a special symbol that is used to carry out some specific operation on its operand. In Dart, we have rich set of built in operators to carry out different type of operations. There are operators for assignment, arithmetic operations, logical operations and comparison operations etc. Operators can be used with many types of variables or constants, but some of the operators are restricted to work on specific data types. Most operators are binary, meaning they take two operands, but a few are unary and only take one operand.

Type of operators in Dart

In Dart, we have following types of operators available –

  • Assignment Operators
  • Arithmetic Operators
  • Type test Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Conditional Operators
  • Casecade notation(..) Operator

Dart Assignment Operators

Assignment operators are used to assign value to a variable, you can assign a variable value or the result of an arithmetical expression. In many cases assignment operator can be combined with other operators to build a shorthand version of a assignment statement are known as Compound Statement. For example, instead of a = a+5 , we can write a += 5.

Dart Assignment operators
Operator Description Expression
= Assignment Operator
+= add and assign a+=b is equivalent to a=a+b
-= subtract and assign a-=b is equivalent to a=a-b
*= multiply and assign a*=b is equivalent to a=a*b
/= divide and assign a/=b is equivalent to a=a/b
~/= divide and assign(Integer) a~/=b is equivalent to a=a~/b
%= mod and assign a%=b is equivalent to a=a%b
<<= Left shift AND assign a<<=5 is equivalent to a=a<<5
>>= Right shift AND assign a>>=5 is equivalent to a=a>>5
&= Bitwise AND assign a&=5 is equivalent to a=a&5
^= Bitwise exclusive OR and assign a^=5 is equivalent to a=a^5
|= Bitwise inclusive OR and assign a|=5 is equivalent to a=a|5

Dart Arithmetic Operators

Arithmetic Operators are used to perform arithmetic operations like addition, subtraction, multiplication, division, %modulus, exponent, etc. Let variable a holds 20 and variable b holds 10, then −

Dart Arithmetic operators
Operator Name Description Example
+ Addition Addition of given operands
a+b returns 30
- Subtraction Subtraction of second operand from first a-b returns 10
-expr Unary Minus
reverse the sign of the expression -(a-b) returns -10
* Multiply Multiplication of given operands a*b returns 200
/ Division Returns Quotient after division
a/b returns 2
~/ Division Return an integer result
a/b returns 2
% Modulus Returns Remainder after division a%b returns 0

Dart Unary Operators (post and pre)

In Java, ++ and — are know as increment and decrement operators respectively. These are unary operators it means they works on single operand. ++ adds 1 to operand and — subtracts 1 to operand respectively. When ++ is used as prefix(like: ++i), ++i will increment the value of i and then return it but, if ++ is used as postfix(like: i++), operator will return the value of operand first and then only increment it.

Operator Example Description
++ [prefix] ++a The value of a after increment
++ [postfix] a++ The value of a before increment
— [prefix] –a The value of a after decrement
— [postfix] a– The value of a before decrement

Dart Type test Operators

The Type test operators are used for checking types at runtime.

Operator Meaning
as Typecast
is True if the object has the specified type
is! False if the object has the specified type

Dart Relational Operators

Relational Operators are used evaluate a comparison between two operands. The result of a relational operation is a Boolean value that can only be true or false. Relational Operators are also referred as Comparison operators.

Let variable a holds 20 and variable b holds 10, then −

Dart Relational operators
Operator Description Example
> greater than a>b returns TRUE
< Less than a<b returns FALSE
>= greater than or equal to a>=b returns TRUE
<= less than or equal to a<=b returns FALSE
== is equal to a==b returns FALSE
!= not equal to a!=b returns TRUE

Dart Logical Operators

Logical operators are used to combine expressions with conditional statements using logical (AND,OR,NOT) operators, which results in true or false. Let variable a holds true or 1 and variable b holds false or 0, then −

Dart Logical operators
Operator Name Description Example
&& Logical AND return true if all expression are true (a && b) returns false
|| Logical OR return true if any expression is true (a || b) returns true
! Logical NOT return complement of expression !a returns false

Dart Bitwise Operators

Bitwise operator are used to perform bit level operation over its operand. Let A = 60; and B = 13;

Binary Equivalent:-

Operator Meaning Example Description
& Binary AND (A & B) It returns 12 which is 0000 1100
| Binary OR (A | B) It returns 12 which is 0000 1100
^ Binary XOR (A ^ B) It returns 49 which is 0011 0001
~ Ones Complement (~A ) It returns -60 which is 1100 0011
<< shift left A << 2 It returns 240 which is 1111 0000
>> shift right A >> 2 It returns 15 which is 0000 1111

Dart Conditional Operators ( ? : )

The conditional operator is considered as short hand for if-else statement. Conditional operator is also called as “Ternary Operator”.

Syntax 1:-

If condition is true the expression will return expr1, if it is not it will return expr2.

Syntax 2:-

If expr1 is non-null, returns its value; otherwise, evaluates and returns the value of expr2.

Dart Cascade notation(..) Operator

Cascades (..) allow you to perform a sequence of operations on the same object. The Cascades notation(..) is similar to method chaining that saves you number of steps and need of temporary variable.

In this tutorial we have learn about the Dart Operators and its application with practical example. I hope you will like this tutorial.