Dart operators , Precendence & associativity

What are operators in Dart ?

In Dart, operators are symbols or keywords that perform specific operations on one or more operands (values or variables). Dart supports various types of operators that can be used for arithmetic, assignment, comparison, logical operations, and more. Here’s an explanation of some common operators in Dart with examples:

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.
+Add (a + b)
Subtract (a – b)
-exprUnary Minus ( use to reverse the sign of the expression)
*Multiply (a * b)
/Divide (a / b)
~/Divides but return an integer result
%Modulus ( outputs remainder of two operands)

void main()
    int x = 14;
    int y = 5;
    // Using ~/ to divide x and y
    var z = x ~/ y;
    print("Quotient of x and y: $z");
    // Remainder of a and b
    var d = x % y;
    print("Remainder of x and y: $d");

Quotient of x and y: 2
Remainder of x and y: 4

Relational Operator and Equality operator

  • Relational operator shows the relation between the two operands.
  • The relational operator can have only two values either true or false.
  • There are six relational operators are there.
  • Equality operator checks whether two objects are equal or not.
  • Same as relational operator they contain Boolean value either true or false.
>Greater than (a > b)
<Less than (a < b)
>=Greater than or equal to (a >= b)
<=Less than or equal to (a <= b)
==Equal to (a == b)
!=Not equal to (a != b)

void main()
    int x = 14;
    int y = 4;
    // Greater between x and y
    var z = x > y;
    print("x is greater than y is $z");
    // Equality between x and y
    var d = x == y;
    print("x and y are equal is $d");


x is greater than y is true
x and y are equal is false

Assignment operators

These operators are used to assign value to operands.

=Equal to ( assign values to the expression)
??=Assignment operator ( assign the value only if it is null )
void main()
    int x = 14;
    int y = 5;
    // Assigning value to variable z
    var z = x / y;
    // Assigning value to variable i
    var i;  //i has a null value
    i ??= x * y; // Value is assign to i as it is null
    // reassign value to i
    i ??= x + y; // Value is not assign because i is not null


Logical operators

These operators are used to combine two or more conditions. A Boolean value is returned by logical operators.

Operators used include:

&&And operator ( returns true if both conditions are true)
||Or operator ( returns true if one of the conditions is true)
!Not Operator ( reverse the output)
void main()
    int x = 14;
    int y = 5;
    // Using And Operator
    bool z = x > 5 && y == 5;
    // Using Or Operator
    bool i = x < 20 || y > 15;
    // Using Not Operator
    bool d = !(x > 5);


Ternary operator or Conditional operator

  • 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()
    int x = 5;
    int y = 7;
    // Conditional Statement
    var z = (x * y > 30) ? "First expression is printed" : "Second expression is printed";

First expression is printed

Type Test Operator

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

These operators are used to perform operand comparisons and are useful for checking types at the runtime of a program. Such operators include:

isis (output a Boolean value true if the object has its specific type)
is!is not (output a Boolean value false if the object has its specific type)
void main() { 
   // Type Test Operator
   String e  = 'Operators in Dart';
   // Using is to compare
   print(e is String);

Bitwise operators

These operators perform bitwise operations on the operands. Below are the operators used:

&Bitwise And (a & b) returns a one in each bit position where both operands’ corresponding bits are ones.
|Bitwise And (a | b) returns a one for each bit position where the matching bits of either or both operands are ones.
^Bitwise XOR (a ^ b) returns a one in each bit location when the corresponding bits of either but not both operands are ones.
~Bitwise NOT( ~a) inverts the operand’s bits.
<<Left shift (a << b) Shifts a in binary representation b (< 32) bits to the left, shifting in zeroes from the right.
>>Right shift (a >> b) shifts a in binary representation b (< 32) bits to the right, eliminating bits shifted off.
void main() { 
   int c = 6; 
   // Bitwise operator
   var bitwise_res =  ~ c;
   print("Bitwise result: $bitwise_res" );
Bitwise result: -7

Cascade notation operators

These operators allow you to perform a sequence of operations on the same element and also perform multiple methods on the same object.

..Cascading method (performs multiple methods on the same object)

class FlutterFever{
    var title;
    var id;
    var date;
    void set(x, y, z)
        this.title = x;
        this.id = y;
        this.date = z;
    void shot()
        var myShot = this.title + this.id + this.date;
void main()
    // Creating objects of class EducativeShot
    FlutterFevershot1 = new FlutterFever();
    FlutterFevershot2 = new FlutterFever();
    // Without using Cascade Notation
    shot1.set('Operators', ' 0017820', ' 15.10.2021');
    // Using Cascade Notation
    shot2..set('Operators in Dart programming by flutterfever.com', ' 0017820', ' 15.10.2021')


Operators 0017820 15.10.2021
Operators in Dart programming by flutterfever.com 0017820 15.10.2021

What is precendence in dart ?

In Dart, operator precedence determines the order in which operators are evaluated when an expression contains multiple operators. It ensures that expressions are evaluated correctly and consistently. Here’s a summary of the operator precedence in Dart, from highest to lowest:

  1. Grouping: () (parentheses)
    • Parentheses are used to explicitly define the order of operations within an expression.
  2. Member Access: . (dot)
    • The dot operator is used to access members (properties and methods) of objects or classes.
  3. Indexing: [] (square brackets)
    • Square brackets are used for indexing, to access elements of lists, maps, or strings.
  4. Function Call: ()
    • Parentheses are used to call functions and pass arguments.
  5. Unary Operators:
    • ! (logical negation)
    • - (negation or subtraction)
  6. Multiplicative Operators:
    • * (multiplication)
    • / (division)
    • % (modulo)
  7. Additive Operators:
    • + (addition)
    • - (subtraction)
  8. Relational Operators:
    • < (less than)
    • > (greater than)
    • <= (less than or equal to)
    • >= (greater than or equal to)
  9. Equality Operators:
    • == (equality)
    • != (inequality)
  10. Logical AND: &&
  11. Logical OR: ||
  12. Conditional Operator: ? : (ternary operator)
  13. Assignment Operators:

The higher the operator appears in the list, the higher its precedence. Operators with higher precedence are evaluated before operators with lower precedence. If operators have the same precedence, the evaluation order is determined by the associativity of the operators (left-to-right or right-to-left).

What is associativity in dart ?

In Dart, associativity determines the order in which operators with the same precedence are evaluated when they appear consecutively in an expression. There are two types of associativity:

  1. Left-to-Right Associativity:
    • Operators with left-to-right associativity are evaluated from left to right when they appear consecutively.
    • Examples of left-to-right associative operators: + (addition), - (subtraction), * (multiplication), / (division), = (assignment), etc.
int result = 10 - 2 - 3; // Evaluates as (10 - 2) - 3 = 5

2.Right-to-Left Associativity:

  • Operators with right-to-left associativity are evaluated from right to left when they appear consecutively.
  • Example of right-to-left associative operator: = (assignment)
int x, y;
x = y = 10; // Evaluates as y = 10 and x = y

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.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More