Operator In Dart
Dart Operators#
Dart includes a variety of operators that allow developers to perform operations on operands. These operators can be categorized into different types, as shown below.
Arithmetic Operators#
+
: Addition-
: Subtraction*
: Multiplication/
: Division~/
: Division returning an integer result%
: Modulo (remainder of division)
Increment and Decrement Operators#
++var
: Pre-incrementvar++
: Post-increment--var
: Pre-decrementvar--
: Post-decrement
Assignment Operators#
=
: Simple assignment+=
: Add and assign-=
: Subtract and assign*=
: Multiply and assign/=
: Divide and assign~/=
: Integer divide and assign%=
: Modulo and assign&=
: AND and assign|=
: OR and assign^=
: XOR and assign>>=
: Right shift and assign<<=
: Left shift and assign
Comparison Operators#
==
: Equal to!=
: Not equal to>
: Greater than<
: Less than>=
: Greater than or equal to<=
: Less than or equal to
Type Test Operators#
is
: True if the object has the specified typeis!
: True if the object does not have the specified type
Logical Operators#
&&
: Logical AND||
: Logical OR!
: Logical NOT
Bitwise and Shift Operators#
&
: Bitwise AND|
: Bitwise OR^
: Bitwise XOR~
: Bitwise NOT (unary bitwise complement)<<
: Left shift>>
: Right shift
Conditional Expressions#
condition ? expr1 : expr2
: Returns expr1 if the condition is true, and expr2 otherwiseexpr1 ?? expr2
: Returns expr1 if it is non-null, otherwise returns expr2
Cascade Notation#
..
: Allows you to perform a sequence of operations on the same object
Other Operators#
()
: Function call[]
: Access element (such as in a list).
: Access a member of an object (method or property)?.
: Conditional member access (if the object is non-null, perform the operation)
Dart Operators Example Code#
Here is a simple Dart program that demonstrates the usage of various Dart operators:
void main() {
// Arithmetic Operators
int a = 10;
int b = 5;
print("Addition: ${a + b}"); // Output: 15
print("Subtraction: ${a - b}"); // Output: 5
print("Multiplication: ${a * b}"); // Output: 50
print("Division: ${a / b}"); // Output: 2.0
print("Integer Division: ${a ~/ b}"); // Output: 2
print("Modulo: ${a % b}"); // Output: 0
// Increment and Decrement
int c = 0;
print("Initial value of c: ${c}"); // Output: 0
c++;
print("Post-increment: ${c}"); // Output: 1
++c;
print("Pre-increment: ${c}"); // Output: 2
c--;
print("Post-decrement: ${c}"); // Output: 1
--c;
print("Pre-decrement: ${c}"); // Output: 0
// Assignment and Compound Assignment
int x = 5;
x += 3;
print("x after += 3: ${x}"); // Output: 8
x *= 2;
print("x after *= 2: ${x}"); // Output: 16
// Comparison Operators
if (a > b) {
print("a is greater than b");
}
// Logical Operators
bool isAdult = true;
bool hasTicket = true;
if (isAdult && hasTicket) {
print("You can enter.");
}
// Conditional Expression
var accessGranted = isAdult && hasTicket ? "Granted" : "Denied";
print("Access: ${accessGranted}"); // Output: "Granted"
// Null-aware Operators
String? name;
print("Name: ${name ?? "Guest"}"); // Output: "Guest"
// Cascade Notation
var list = [];
list..add('Apple')..add('Banana')..add('Cherry');
print("List items: ${list}"); // Output: [Apple, Banana, Cherry]
}