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.
- Arithmetic Operators
Arithmetic operators perform basic mathematical operations.
+: Addition
-: Subtraction
*: Multiplication
/: Division (returns double)
~/: Integer division (returns int)
%: Modulo (remainder)
- 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
- 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
- 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)
- Bitwise Operators
These operators work on integers at the bit level.
&: Bitwise AND
|: Bitwise OR
^: Bitwise XOR
~: Bitwise NOT (unary)
<<: Left shift
>>: Right shift
- Assignment Operators
Assign values to variables. Compound operators combine assignment with another operation.
=: Simple assignment
+=: Add and assign (e.g.,a += bmeansa = 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
- Conditional (Ternary) Operator
The ternary operator condition ? expr1 : expr2 evaluates to expr1 if condition is true, otherwise expr2.
- 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.
- 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)
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.
Complete Example
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
isandis!to check types at runtime.
- Use
- The cascade operator
..lets you chain operations on the same object.
- The cascade operator
- Null‑aware operators (
?.,??,??=) help you write safe code with nullable variables.
- Null‑aware operators (
- Operator precedence can be controlled with parentheses.