Dart Operators


In Dart, operators are special symbols that perform operations on variables and values. Dart supports a wide range of operators, which can be categorized into several types based on their functionality. Here’s an overview of the different types of operators available in Dart:

1. Arithmetic Operators

These operators perform basic mathematical operations.

OperatorDescriptionExample
+Addition5 + 3 (results in 8)
-Subtraction5 - 3 (results in 2)
*Multiplication5 * 3 (results in 15)
/Division5 / 2 (results in 2.5)
~/Integer Division5 ~/ 2 (results in 2)
%Modulus (Remainder)5 % 2 (results in 1)

2. Relational Operators

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

OperatorDescriptionExample
==Equal to5 == 3 (results in false)
!=Not equal to5 != 3 (results in true)
>Greater than5 > 3 (results in true)
<Less than5 < 3 (results in false)
>=Greater than or equal to5 >= 5 (results in true)
<=Less than or equal to5 <= 3 (results in false)

3. Logical Operators

These operators are used to perform logical operations, returning a boolean value.

OperatorDescriptionExample
&&Logical AND(5 > 3) && (3 < 4) (results in true)
``
!Logical NOT!(5 > 3) (results in false)

4. Assignment Operators

These operators are used to assign values to variables.

OperatorDescriptionExample
=Assigns a valueint a = 5;
+=Add and assigna += 3; (equivalent to a = a + 3)
-=Subtract and assigna -= 2; (equivalent to a = a - 2)
*=Multiply and assigna *= 2; (equivalent to a = a * 2)
/=Divide and assigna /= 2; (equivalent to a = a / 2)
~/=Integer divide and assigna ~/= 2; (equivalent to a = a ~/ 2)
%=Modulus and assigna %= 2; (equivalent to a = a % 2)

5. Conditional Operator

The conditional operator (?:) is a shorthand for an if-else statement, often referred to as the ternary operator.

OperatorDescriptionExample
?:Ternary (Conditional)var result = (a > b) ? a : b; (assigns the greater of a or b to result)

6. Type Test Operators

These operators are used to check the type of a variable.

OperatorDescriptionExample
isChecks if an object is of a specific typeif (x is String) (checks if x is a String)
is!Checks if an object is not of a specific typeif (x is! String) (checks if x is not a String)

7. Bitwise Operators

These operators perform operations on the binary representations of integers.

OperatorDescriptionExample
&Bitwise AND5 & 3 (results in 1)
``Bitwise OR
^Bitwise XOR5 ^ 3 (results in 6)
~Bitwise NOT~5 (results in -6)
<<Left shift5 << 1 (results in 10)
>>Right shift5 >> 1 (results in 2)

8. Cascade Operator

The cascade operator (..) allows you to make a series of calls on the same object without having to repeat the object reference.

Example:

void main() { var list = []..add(1)..add(2)..add(3); print(list); // Output: [1, 2, 3] }

Summary

Dart provides a comprehensive set of operators that allow developers to perform various operations on data. Understanding how to use these operators effectively is essential for writing clear and concise Dart code. Whether you’re performing arithmetic calculations, making comparisons, or manipulating data structures, operators play a crucial role in controlling the flow and logic of your Dart programs.