Question #174EasyDart Basics

What are all the different types of operator in dart or flutter ?

#flutter#dart

Answer

Overview

Dart has a rich set of operators grouped into categories: arithmetic, relational, logical, bitwise, assignment, type, null-aware, and more.


1. Arithmetic Operators

dart
int a = 10, b = 3;
print(a + b);    // 13 -- Addition
print(a - b);    // 7  -- Subtraction
print(a * b);    // 30 -- Multiplication
print(a / b);    // 3.333 -- Division (returns double)
print(a ~/ b);   // 3  -- Integer division
print(a % b);    // 1  -- Modulo (remainder)
print(-a);       // -10 -- Negation

// Increment / Decrement
int x = 5;
print(x++); // 5 (post-increment: returns then increments)
print(++x); // 7 (pre-increment: increments then returns)
print(x--); // 7 (post-decrement)
print(--x); // 5 (pre-decrement)

2. Relational (Comparison) Operators

dart
print(5 > 3);   // true
print(5 < 3);   // false
print(5 >= 5);  // true
print(5 <= 4);  // false
print(5 == 5);  // true
print(5 != 3);  // true

3. Logical Operators

dart
print(true && false); // false -- AND
print(true || false); // true  -- OR
print(!true);         // false -- NOT

// Short circuit
var x = 0;
false && (x = 1);  // Right side never evaluated
print(x);          // 0

true || (x = 2);   // Right side never evaluated
print(x);          // 0

4. Bitwise Operators

dart
print(5 & 3);   // 1  -- AND  (101 & 011 = 001)
print(5 | 3);   // 7  -- OR   (101 | 011 = 111)
print(5 ^ 3);   // 6  -- XOR  (101 ^ 011 = 110)
print(~5);      // -6 -- NOT
print(5 << 1);  // 10 -- Left shift
print(5 >> 1);  // 2  -- Right shift

5. Assignment Operators

dart
int x = 10;
x += 5;   // x = x + 5 = 15
x -= 3;   // x = x - 3 = 12
x *= 2;   // x = x * 2 = 24
x ~/= 5;  // x = x ~/ 5 = 4
x %= 3;   // x = x % 3 = 1

6. Null-Aware Operators

dart
// ?? -- Null coalescing
String? name = null;
print(name ?? 'Guest'); // Guest

// ??= -- Null assignment
name ??= 'Anonymous';   // Only assigns if null

// ?. -- Null-aware access
print(name?.length);    // null if name is null

// ! -- Null assertion (throws if null)
print(name!.length);    // Crashes if null

// ...? -- Null-aware spread
List<int>? extras = null;
final list = [1, 2, ...?extras]; // [1, 2]

7. Type Test Operators

dart
dynamic value = 'Hello';
print(value is String);    // true
print(value is! int);      // true (is not)

// as -- Type cast
final str = value as String; // Cast (throws if wrong type)

8. Conditional Operators

dart
// Ternary
int x = 5;
String result = x > 0 ? 'positive' : 'non-positive';

// Cascade (..)
Paint()
  ..color = Colors.red
  ..strokeWidth = 2
  ..style = PaintingStyle.stroke;

Operator Precedence (High to Low)

text
Unary:   -expr, !expr, ++, --
Type:    as, is, is!
Mult:    *, /, ~/, %
Add:     +, -
Shift:   <<, >>
Bitwise: &, ^, |
Relat:   <, >, <=, >=
Eq:      ==, !=
Logical: &&, ||
Null:    ??
Ternary: ? :
Assign:  =, +=, ??=, etc.

Most Used:

text
+
,
text
-
,
text
*
,
text
/
,
text
==
,
text
!=
,
text
&&
,
text
||
,
text
!
,
text
??
,
text
?.
,
text
!
(null assertion).