Open In App

Operators in Dart

Last Updated : 24 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The operators are special symbols used to perform certain operations on operands. Dart has numerous built-in operators that can be utilized for various functions; for example, '+' is used to add two operands. Operators are designed to execute operations on one or two operands. 

Basics-of-Dart

Precedence Table of Operators in Dart

Description

Operator

Associativity

unary postfix

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

Right

unary prefix

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

Right

multiplicative

* / % ~/

Left

additive

+ -

Left

shift

<< >> >>>

Left

bitwise AND

&

Left

bitwise XOR

^

Left

bitwise OR

|

Left

relational and type test

>= > <= < as is is!

Left

equailty

== !=

Left

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
+AdditionUsed to add two operands
-SubtractionUsed to subtract two operands
-exprUnary MinusUsed to reverse the sign of the expression
*MultiplyUsed to multiply two operands
/DivisionUsed to divide two operands
~/DivisionUsed to divide two operands but give output in integer(returns quotient)
%ModulusUsed to give remainder of two operands(returns remainder)


Example:

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: 

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:

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.

Example:

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 one operand.
<<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 the right, preserving the sign bit (inserting 0 for positive numbers and 1 for negative numbers).

>>>

Unsigned Shift right

Shifts a in binary representation to b bits to the right, inserting 0s (ignores sign).


Example:

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 a value to a expression or variable
??=Assignment operatorAssign a value only if the variable is null.


Example:

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 OperatorReturns true only if both conditions are true.
||Or OperatorReturns true if at least one condition is true.
!Not OperatorReverses the boolean value of an expression.

Example 1 : 

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:

compileDDC
main.dart:7:15: Error: A value of type 'int' can't be assigned to a variable of type 'bool'.
print(a && b);
^
main.dart:7:20: Error: A value of type 'int' can't be assigned to a variable of type 'bool'.
print(a && b);
^
main.dart:10:15: Error: A value of type 'int' can't be assigned to a variable of type 'bool'.
print(a || b);
^
main.dart:10:20: Error: A value of type 'int' can't be assigned to a variable of type 'bool'.
print(a || b);
^
main.dart:13:16: Error: A value of type 'int' can't be assigned to a variable of type 'bool'.
print(!a);

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 ? expression1 : expersion2Conditional OperatorA shorter version of if-else. If condition is true, executes expr1; otherwise, executes expr2
expersion1 ?? exppression2Conditional OperatorIf expr1 is non-null, returns its value; otherwise, returns expr2.


Example:

Dart
void main()
{
        int a = 5;
    
        // 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 MethodUsed to perform multiple method calls or property assignments on the same object.

..?

Null Shorting Cascade

Used to perform multiple operations on an object only if it is not null. Prevents null reference errors


Example:

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

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

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

    void add() {
        if (a != null && b != null) { // Null safety check
            var z = a! + b!;
            print(z);
        } else {
            print("Values are not initialized.");
        }
    }
}

void main() {
    // Creating objects of class GFG
    GFG geek1 = GFG();
    GFG geek2 = 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


Next Article
Article Tags :

Similar Reads