Mastering Operators in Dart: A Beginner's Guide
Operators are fundamental to any programming language, and Dart is no exception. They enable you to perform operations on variables and values, making your code dynamic and functional. In this blog, we’ll dive into the types of operators in Dart, complete with examples to make learning easy for beginners.
What are Operators?
Operators are special symbols or keywords that perform operations on one or more operands (values or variables). For example:
int sum = 5 + 3; // Here, '+' is the operator.
Dart provides a variety of operators that can be categorized into several types.
1. Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations like addition, subtraction, multiplication, and division.
Examples
void main() {
int a = 10;
int b = 3;
print('Addition: ${a + b}'); // Output: 13
print('Subtraction: ${a - b}'); // Output: 7
print('Multiplication: ${a * b}'); // Output: 30
print('Division: ${a / b}'); // Output: 3.3333333333333335
print('Modulus: ${a % b}'); // Output: 1
}
2. Relational (Comparison) Operators
Relational operators compare two values and return a boolean result (`true` or `false`).
Examples
void main() {
int x = 10;
int y = 20;
print('Equal to: ${x == y}'); // Output: false
print('Not equal to: ${x != y}'); // Output: true
print('Greater than: ${x > y}'); // Output: false
print('Less than: ${x < y}'); // Output: true
print('Greater or equal: ${x >= y}'); // Output: false
print('Less or equal: ${x <= y}'); // Output: true
}
3. Logical Operators
Logical operators are used to combine multiple boolean expressions.
Examples
void main() {
bool a = true;
bool b = false;
print('AND: ${a && b}'); // Output: false
print('OR: ${a || b}'); // Output: true
print('NOT: ${!a}'); // Output: false
}
4. Assignment Operators
Assignment operators are used to assign values to variables. They can also combine assignment with arithmetic operations.
Examples
void main() {
int x = 10;
x += 5; // Same as x = x + 5;
print('x += 5: ${x}'); // Output: 15
x -= 3; // Same as x = x - 3;
print('x -= 3: ${x}'); // Output: 12
x *= 2; // Same as x = x * 2;
print('x *= 2: ${x}'); // Output: 24
x ~/= 4; // Same as x = x ~/ 4 (integer division);
print('x ~/= 4: ${x}'); // Output: 6
}
5. Unary Operators
Unary operators operate on a single operand.
Examples
void main() {
int x = 5;
print('Unary minus: ${-x}'); // Output: -5
print('Increment: ${++x}'); // Output: 6 (pre-increment)
print('Decrement: ${--x}'); // Output: 5 (pre-decrement)
int y = 10;
print('Post-increment: ${y++}'); // Output: 10
print('After post-increment: ${y}'); // Output: 11
}
6. Bitwise Operators
Bitwise operators perform operations on binary representations of integers.
Examples
void main() {
int a = 5; // Binary: 0101
int b = 3; // Binary: 0011
print('AND: ${a & b}'); // Output: 1 (Binary: 0001)
print('OR: ${a | b}'); // Output: 7 (Binary: 0111)
print('XOR: ${a ^ b}'); // Output: 6 (Binary: 0110)
print('NOT: ${~a}'); // Output: -6 (Binary: 1010 in two's complement)
}
7. Conditional (Ternary) Operator
The conditional operator evaluates a condition and returns one of two values based on the result.
Syntax
condition ? valueIfTrue : valueIfFalse;
Example
void main() {
int age = 18;
String eligibility = (age >= 18) ? 'Eligible to vote' : 'Not eligible to vote';
print(eligibility); // Output: Eligible to vote
}
8. Null-aware Operators
Null-aware operators help handle null values gracefully.
Examples
void main() {
String? name;
// Assign a default value if null
String displayName = name ?? 'Guest';
print(displayName); // Output: Guest
name = 'Alice';
print(name ?? 'Guest'); // Output: Alice
}
Conclusion
Operators in Dart provide the tools you need to manipulate data, make decisions, and handle logic in your applications. From arithmetic and logical operations to null-aware and bitwise manipulations, understanding these operators is key to writing efficient Dart code.
Practice these operators with real-world scenarios to solidify your grasp. Happy coding!

