flutter
/

Dart Operators – Arithmetic, Logical, Relational and More

Last Sync: Today

On this page

13
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

flutter

Dart Operators – Arithmetic, Logical, Relational and More

What are Operators?

Operators are special symbols used to perform operations on operands (values and variables). Dart supports a rich set of operators similar to other C‑style languages. Understanding operators is essential for writing expressions and controlling program flow.

  1. Arithmetic Operators

Arithmetic operators perform basic mathematical operations.

    • + : Addition
    • - : Subtraction
    • * : Multiplication
    • / : Division (returns double)
    • ~/ : Integer division (returns int)
    • % : Modulo (remainder)
DARTRead-only
1
void main() {
  int a = 10, b = 3;
  print(a + b);   // 13
  print(a - b);   // 7
  print(a * b);   // 30
  print(a / b);   // 3.3333333333333335 (double)
  print(a ~/ b);  // 3 (integer division)
  print(a % b);   // 1 (remainder)
}

  1. Relational (Comparison) Operators

These operators compare two values and return a boolean (true or false).

    • == : Equal to
    • != : Not equal to
    • > : Greater than
    • < : Less than
    • >= : Greater than or equal to
    • <= : Less than or equal to
DARTRead-only
1
void main() {
  int x = 5, y = 8;
  print(x == y); // false
  print(x != y); // true
  print(x > y);  // false
  print(x < y);  // true
  print(x >= 5); // true
  print(y <= 8); // true
}

  1. Type Test Operators

These operators check the type of an object at runtime.

    • is : True if the object has the specified type
    • is! : True if the object does not have the specified type
DARTRead-only
1
void main() {
  var value = 'Hello';
  print(value is String);   // true
  print(value is int);      // false
  print(value is! bool);    // true
}

  1. Logical Operators

Logical operators combine boolean expressions.

    • && : Logical AND (true if both operands are true)
    • || : Logical OR (true if at least one operand is true)
    • ! : Logical NOT (inverts the boolean value)
DARTRead-only
1
void main() {
  bool sunny = true, warm = false;
  print(sunny && warm); // false
  print(sunny || warm); // true
  print(!sunny);        // false
}

  1. Bitwise Operators

These operators work on integers at the bit level.

    • & : Bitwise AND
    • | : Bitwise OR
    • ^ : Bitwise XOR
    • ~ : Bitwise NOT (unary)
    • << : Left shift
    • >> : Right shift
DARTRead-only
1
void main() {
  int a = 5; // binary 0101
  int b = 3; // binary 0011
  print(a & b);  // 0001 -> 1
  print(a | b);  // 0111 -> 7
  print(a ^ b);  // 0110 -> 6
  print(a << 1); // 1010 -> 10
  print(a >> 1); // 0010 -> 2
}

  1. Assignment Operators

Assign values to variables. Compound operators combine assignment with another operation.

    • = : Simple assignment
    • += : Add and assign (e.g., a += b means a = a + b)
    • -= : Subtract and assign
    • *= : Multiply and assign
    • /= : Divide and assign
    • ~/= : Integer divide and assign
    • %= : Modulo and assign
    • <<= : Left shift and assign
    • >>= : Right shift and assign
    • &= : Bitwise AND and assign
    • ^= : Bitwise XOR and assign
    • |= : Bitwise OR and assign
DARTRead-only
1
void main() {
  int a = 10;
  a += 5;  // a = a + 5 -> 15
  print(a);
  a ~/= 2; // a = a ~/ 2 -> 7
  print(a);
}

  1. Conditional (Ternary) Operator

The ternary operator condition ? expr1 : expr2 evaluates to expr1 if condition is true, otherwise expr2.

DARTRead-only
1
void main() {
  int age = 18;
  String status = (age >= 18) ? 'Adult' : 'Minor';
  print(status); // Adult
}

  1. Cascade Notation (..)

The cascade operator allows you to perform multiple operations on the same object. It returns the original object, not the result of each operation.

DARTRead-only
1
class Person {
  String name = '';
  int age = 0;
  void sayHello() => print('Hi, I am $name');
}

void main() {
  Person person = Person()
    ..name = 'Alice'
    ..age = 30
    ..sayHello();
}

  1. Null‑Aware Operators

Dart provides special operators to handle nullable values safely.

    • ?. : Safe member access (returns null if the object is null)
    • ?? : If‑null operator (returns left side if not null, else right side)
    • ??= : Null‑aware assignment (assigns only if the variable is null)
DARTRead-only
1
void main() {
  String? name;
  print(name?.length);   // null (safe access)
  
  String displayName = name ?? 'Guest';
  print(displayName);    // Guest
  
  name ??= 'Alice';
  print(name);           // Alice (assigned because it was null)
}

Operator Precedence

Operators have a defined precedence that determines the order in which they are evaluated in an expression. Use parentheses () to override precedence and make expressions clearer.

DARTRead-only
1
void main() {
  int result = 5 + 3 * 2;   // 5 + (3 * 2) = 11
  print(result);
  result = (5 + 3) * 2;     // (5 + 3) * 2 = 16
  print(result);
}

Complete Example

DARTRead-only
1
void main() {
  // Mixing various operators
  int a = 15, b = 4;
  double c = 10.5;
  
  // Arithmetic + relational + logical
  if ((a ~/ b > 3) && (c > 10)) {
    print('Condition met');
  }
  
  // Ternary + assignment
  int min = (a < b) ? a : b;
  print('Minimum: $min');
  
  // Cascade on a list
  var numbers = [1, 2, 3]
    ..add(4)
    ..removeAt(0);
  print(numbers); // [2, 3, 4]
}

Key Takeaways

    • Operators are symbols that perform operations on one or more operands.
    • Dart includes arithmetic, relational, type test, logical, bitwise, assignment, conditional, cascade, and null‑aware operators.
    • Use is and is! to check types at runtime.
    • The cascade operator .. lets you chain operations on the same object.
    • Null‑aware operators (?., ??, ??=) help you write safe code with nullable variables.
    • Operator precedence can be controlled with parentheses.

Try it yourself

void main() {
  // Experiment with operators
  int x = 10, y = 3;
  print('x + y = ${x + y}');
  print('x ~/ y = ${x ~/ y}');
  print('x > y = ${x > y}');
  print('x is int = ${x is int}');
}

Test Your Knowledge

Q1
of 4

What is the output of `print(10 ~/ 3);`?

A
3.333
B
3
C
3.0
D
Error
Q2
of 4

Which operator checks if an object is of a specific type?

A
==
B
is
C
as
D
typeof
Q3
of 4

What does the `??` operator do?

A
Returns the left operand if it's not null, otherwise the right operand
B
Returns the right operand if left is null
C
Performs a null check and throws an error
D
Assigns a value only if the variable is null
Q4
of 4

Which operator is used to chain multiple method calls on the same object?

A
..
B
.
C
?.
D
=>

Frequently Asked Questions

What is the difference between `/` and `~/` in Dart?

The / operator performs floating-point division and always returns a double. The ~/ operator performs integer division, truncating any fractional part and returning an int. For example, 10 / 3 = 3.333..., while 10 ~/ 3 = 3.

How do I test if an object is of a certain type?

Use the is operator: if (obj is String) { ... }. To test that it is NOT a certain type, use is!: if (obj is! int) { ... }.

What is the null-aware operator `??` used for?

The ?? operator returns the left operand if it's not null; otherwise, it returns the right operand. It's commonly used to provide default values for nullable expressions: String name = userName ?? 'Guest';.

Can I use `&&` and `||` with non-boolean values?

No, in Dart logical operators && and || work only with boolean expressions. If you need to handle other types, you must convert them to bool (e.g., using != null or explicit comparison).

What is the cascade operator `..` and how does it work?

The cascade operator allows you to perform multiple operations on the same object without repeating the object reference. It returns the original object, enabling method chaining. For example: list..add(1)..add(2)..remove(1);

How do I combine assignment with an operation?

Dart provides compound assignment operators like +=, -=, *=, etc. For example, a += 5 is equivalent to a = a + 5. These work for most operators, including bitwise (&=, |=, ^=).

What is operator precedence and why does it matter?

Operator precedence determines the order in which operators are evaluated in an expression. For example, multiplication has higher precedence than addition, so 2 + 3 * 4 is evaluated as 2 + (3 * 4) = 14. You can use parentheses () to override precedence.

How do I check if two objects are equal?

Use the == operator. Note that in Dart, == checks for value equality (calls the == method of the object). For reference equality (whether two references point to the same object), use the identical() function.

Previous

dart variables

Next

dart conditions

Related Content

Need help?

Explore our comprehensive docs or start a chat with our tech experts.