Open In App

Operators in Dart

Last Updated : 22 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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. 

Basics-of-Dart

Precedence Table of Operators in Dart

Description

Operator

Associativity

unary postfix

expr++ expr– () [] ?[] . ?. !

None

unary prefix

-expr !expr ~expr ++expr –expr await expr

None

multiplicative

* / % ~/

Left

additive

+ –

Left

shift

<< >> >>>

Left

bitwise AND

&

Left

bitwise XOR

^

Left

bitwise OR

|

Left

relational and type test

>= > <= < as is is!

None

equailty

== !=

None

logical AND

&&

Left

logical OR

||

Left

if-null

??

Left

conditional

expr ? expr2 : expr3

Right

cascade

.. ?..

Left

assignment

= *= /= += -= &= ^= etc.

Right

Different types of operators in Dart

The following are the various types of operators in Dart:

1. Arithmetic Operators

This class of operators contain those operators which are used to perform arithmetic operation on the operands. They are binary operators i.e they act on two operands. They go like this: 

Operator SymbolOperator NameOperator Description
+AdditionUse to add two operands
SubtractionUse to subtract two operands
-exprUnary MinusIt is Use to reverse the sign of the expression
*MultiplyUse to multiply two operands
/DivisionUse to divide two operands
~/DivisionUse to divide two operands but give output in integer(returns quotient)
%ModulusUse to give remainder of two operands(returns remainder)

Example showing the use of all Arithmetic Operator:

Dart
// Dart Program Demonstrating use
// Of all Arithmetic Operators

void main()
{
    int a = 2;
    int b = 3;

    // Adding a and b
    var c = a + b;
    print("Sum  (a + b) = $c");

    // Subtracting a and b
    var d = a - b;
    print("Difference (a - b) = $d");

    // Using unary minus
    var e = -d;
    print("Negation -(a - b) = $e");

    // Multiplication of a and b
    var f = a * b;
    print("Product (a * b) = $f");

    // Division of a and b
    var g = b / a;
    print("Division (b / a) = $g");

    // Using ~/ to divide a and b
    var h = b ~/ a;
    print("Quotient (b ~/ a) = $h");

    // Remainder of a and b
    var i = b % a;
    print("Remainder (b % a) = $i");
}

Output: 

Sum  (a + b) = 5
Difference (a - b) = -1
Negation -(a - b) = 1
Product (a * b) = 6
Division (b / a) = 1.5
Quotient (b ~/ a) = 1
Remainder (b % a) = 1

2. Relational Operators

This class of operators contain those operators which are used to perform relational operation on the operands. It goes like this: 

Operator SymbolOperator NameOperator Description
>Greater thanCheck which operand is bigger and give result as boolean expression.
<Less thanCheck which operand is smaller and give result as boolean expression.
>=Greater than or equal toCheck which operand is greater or equal to each other and give result as boolean expression.
<=less than equal toCheck which operand is less than or equal to each other and give result as boolean expression.
==Equal toCheck whether the operand are equal to each other or not and give result as boolean expression.
!=Not Equal toCheck whether the operand are not equal to each other or not and give result as boolean expression.

Example showing the use of Relational Operators: 

Dart
// Dart Program Demonstrating use
// Of all Relational Operators
void main()
{
    int a = 2;
    int b = 3;

    // Greater between a and b
    var c = a > b;
    print("a is greater than b (a > b) : $c");

    // Smaller between a and b
    var d = a < b;
    print("a is smaller than b (a < b) : $d");

    // Greater than or equal to between a and b
    var e = a >= b;
    print("a is greater than b (a >= b) : $e");

    // Less than or equal to between a and b
    var f = a <= b;
    print("a is smaller than b (a <= b) : $f");

    // Equality between a and b
    var g = b == a;
    print("a and b are equal (b == a) : $g");

    // Unequality between a and b
    var h = b != a;
    print("a and b are not equal (b != a) : $h");
}

Output: 

a is greater than b (a > b) : false
a is smaller than b (a < b) : true
a is greater than b (a >= b) : false
a is smaller than b (a <= b) : true
a and b are equal (b == a) : false
a and b are not equal (b != a) : true

Note: == operator can’t be used to check if the object is same. So, to check if the object are same we use identical() function.

3. Type Test Operators

This class of operators contain those operators which are used to perform comparison on the operands. It goes like this: 

Operator SymbolOperator NameOperator Description
isisGives boolean value true as output if the object has specific type
is!is notGives boolean value false as output if the object has specific type

Example: Using Type Test Operators in the program 

Dart
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
// 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

4. Bitwise Operators

This class of operators contain those operators which are used to perform bitwise operation on the operands. It goes like this: 

Operator SymbolOperator NameOperator Description
&Bitwise ANDPerforms bitwise AND operation on two operands.
|Bitwise ORPerforms bitwise OR operation on two operands.
^Bitwise XORPerforms bitwise XOR operation on two operands.
~Bitwise NOTPerforms bitwise NOT operation on two operands.
<<Left ShiftShifts a in binary representation to b bits to left and inserting 0 from right.
>>Right ShiftShifts a in binary representation to b bits to left and inserting 0 from left.

>>>

Unsigned Shift right

Shifts a in binary representation to b bits to left (it ignores sign).

Example: Using Bitwise Operators in the program 

Dart
// Dart Program to Demonstrate
// Use of Dart Bitwise Operators
void main()
{
      print("Demonstrate use of Dart Bitwise Operators");
  
    int a = 5;
    int b = 7;

    // Performing Bitwise AND on a and b
    var c = a & b;
    print("a & b : $c");

    // Performing Bitwise OR on a and b
    var d = a | b;
    print("a | b : $d");

    // Performing Bitwise XOR on a and b
    var e = a ^ b;
    print("a ^ b : $e");

    // Performing Bitwise NOT on a
    var f = ~a;
    print("~a : $f");

    // Performing left shift on a
    var g = a << b;
    print("a << b : $g");

    // Performing right shift on a
    var h = a >> b;
    print("a >> b : $h");
  
      var i = -a >>> b;
      print("-a >>> b : $i");
}

Output: 

Demonstrate use of Dart Bitwise Operators
a & b : 5
a | b : 7
a ^ b : 2
~a : 4294967290
a << b : 640
a >> b : 0
-a >>> b : 33554431

5. Assignment Operators

This class of operators contain those operators which are used to assign value to the operands. It goes like this: 

Operator SymbolOperator NameOperator Description
=Equal toUse to assign values to the expression or variable
??=Assignment operatorAssign the value only if it is null.

Example: Using Assignment Operators in the program 

Dart
void main()
{
    int a = 5;
    int b = 7;

    // Assigning value to variable c
    var c = a * b;
  
    print("assignment  operator used c = a*b so now c = $c\n");

    // Assigning value to variable d
    var d;
  
      // Value is assign as it is null
    d ??= a + b;
  
    print("Assigning value only if d is null");
    print("d??= a+b so d = $d \n");
    
    // Again trying to assign value to d
    d ??= a - b;
      // Value is not assign as it is not null
  
    print("Assigning value only if d is null");
    print("d??= a-b so d = $d");
    print("As d was not null value was not updated");
}

Output: 

assignment  operator used c = a*b so now c = 35

Assigning value only if d is null
d??= a+b so d = 12

Assigning value only if d is null
d??= a-b so d = 12
As d was not null value was not updated

Compound Assignment Operator

Apart from there is another way where we can use a operator that is compound assignment operator where we combine an operator with an assignment operatorso to shorten the steps and make code more effective.

Compound_Assignment

Example:

a+=1;

// The above statement is same as
// the statement mentioned below
a=a+1;

6. Logical Operators

This class of operators contain those operators which are used to logically combine two or more conditions of the operands. It goes like this: 

Operator SymbolOperator NameOperator Description
&&And OperatorUse to add two conditions and if both are true than it will return true.
||Or OperatorUse to add two conditions and if even one of them is true than it will return true.
!Not OperatorIt is use to reverse the result.

Example 1: Using Logical Operators in the program 

Dart
void main()
{
    int a = 5;
    int b = 7;

    // Using And Operator
    bool c = a > 10 && b < 10;
    print(c);

    // Using Or Operator
    bool d = a > 10 || b < 10;
    print(d);

    // Using Not Operator
    bool e = !(a > 10);
    print(e);
}

Output: 

false
true
true

Note: Logical operator can only be application to boolean expression and in dart, non-zero numbers are not considered as true and zero as false

Example 2: (Incorrect Way)

Dart
void main()
{
    int a = 5;
    int b = 7;

    // Using And Operator
    print(a && b);

    // Using Or Operator
    print(a || b);

    // Using Not Operator
    print(!a);
}

Output:

Error: A value of type 'int' can't be assigned to a variable of type 'bool'.

Example 3: (Correct Way)

Dart
// Dart Program to demonstrate use of
// Logical Operators

void main()
{
    var a = true;
    var b = false;
      // Printing the Values of a & b
    print("a: $a , b: $b\n");

    // Using And Operator
    print("a && b = ${a&&b}");

    // Using Or Operator
    print("a || b = ${a||b}");

    // Using Not Operator
    print("!a = ${!a}");
}

Output:

a: true , b: false

a && b = false
a || b = true
!a = false

7. Conditional Operators

This class of operators contain those operators which are used to perform comparison on the operands. It goes like this: 

Operator SymbolOperator NameOperator Description
condition ? expersion1 : expersion2Conditional OperatorIt is a simple version of if-else statement. If the condition is true than expersion1 is executed else expersion2 is executed.
expersion1 ?? expersion2Conditional OperatorIf expersion1 is non-null returns its value else returns expression2 value.

Example: Using Conditional Operators in the program 

Dart
void main()
{
    int a = 5;
    int b = 7;

    // Conditional Statement
    var c = (a < 10) ? "Statement is Correct, Geek" : "Statement is Wrong, Geek";
    print(c);

    
    // Conditional statement
      int? n;
      // Warning: Operand of null-aware operation '??' has type 'int' which excludes null.
      // For batter practice make both same type to avoid warning
      // var d = n ?? 10;
      var d = n ?? "n has Null value";
      print(d);

    // After assigning value to n
    n = 10;
      // we make it all ready null safe 
    //d = n ? ? "n has Null value";
      d = n;
    print(d);
}

Output: 

Statement is Correct, Geek
n has Null value
10

Note: Also In The Above Code,You May Notice That The Variable ‘n’ is Declared As “int? n”.By declaring n as int?, you are indicating that the variable n can hold an integer value or a null value. The ? denotes that the variable is nullable, meaning it can be assigned a null value in addition to integer values.

8. Cascade Notation Operators:

This class of operators allows you to perform a sequence of operation on the same element. It allows you to perform multiple methods on the same object. It goes like this: 

Operator SymbolOperator NameOperator Description
..Cascading MethodIt is used to perform multiple methods on the same object.

..?

Null Shorting Cascade

It is used when we are sure that the none of the cascade operations are done on Null elements.

Example: Using Cascade Notation Operators in the program 

Dart
// Dart Program to Demonstrate
// Use of Cascading Operator

// Class 
class GFG {
    var? a;
    var? b;

    void set(x, y)
    {
        this.a = x;
        this.b = y;
    }

    void add()
    {
        var z = this.a + this.b;
        print(z);
    }
}

void main()
{
    // Creating objects of class GFG
    GFG geek1 = new GFG();
    GFG geek2 = new GFG();

    // Without using Cascade Notation
    geek1.set(1, 2);
    geek1.add();

    // Using Cascade Notation
    geek2..set(3, 4)
         ..add();
}

Output: 

3
7

To know more about Dart please check Dart Tutorial



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads