The Ultimate Guide to Dart Operators

Dart Operators

Introduction of Dart Operators

Dart Operators are special symbols or phrases that you use to check, change, or combine values. Here’s an overview of the types of operators available in Dart, along with a brief description and example for each:

1. Arithmetic Operators
Perform mathematical operations like addition, subtraction, multiplication, and division.

int sum = 10 + 5; // 15
int difference = 10 – 5; // 5
int product = 10 * 5; // 50
double quotient = 10 / 5; // 2.0

2. Equality and Relational Operators
Compare two values, yielding a boolean value (true or false).

bool isEqual = (10 == 5); // false
bool isNotEqual = (10 != 5); // true
bool isGreaterThan = (10 > 5); // true
bool isLessThan = (10 < 5); // false
bool isGreaterThanOrEqual = (10 >= 5); // true
bool isLessThanOrEqual = (10 <= 5); // true

3. Type Test Operators
Used to check the type of a variable.

var name = ‘Alice’;
bool isString = name is String; // true
bool isNotInt = name is! int; // true

4. Assignment Operators
Used to assign values to a variable.

int a = 10;
a += 5; // now a is 15
a -= 5; // now a is 10
a *= 2; // now a is 20
a /= 2; // now a is 10

5. Logical Operators
Used to combine boolean values.

bool isTrue = true;
bool isFalse = false;
bool result = isTrue && isFalse; // false (logical AND)
result = isTrue || isFalse; // true (logical OR)
result = !isTrue; // false (logical NOT)

6. Bitwise and Shift Operators
Operate on the binary representations of integers.

int x = 5; // binary 0101
int y = 3; // binary 0011
int result = x & y; // binary 0001, which is 1
result = x | y; // binary 0111, which is 7
result = x ^ y; // binary 0110, which is 6
result = x << 2; // binary 10100, which is 20
result = x >> 1; // binary 0010, which is 2

7. Conditional Operator
A shorthand for if-else, which is used for conditional expressions.

int a = 10, b = 5;
int smallerNumber = a < b ? a : b; // 5

8. Cascade Operator
Allows you to make a sequence of operations on the same object.

List<int> numbers = [];
numbers..add(1)..add(2)..add(3);

Conclusion Dart Opertators

Understanding these dart operators is essential for manipulating data and implementing logic in your Dart applications effectively. Dart Operators enable concise and powerful expressions that can handle everything from basic arithmetic to complex logical conditions.

Thank for visiting Hybrid App Development
Posted by Hussam HM

Premium Partner

WordPress Wallah offer's premium WordPress themes, plugins, and Template Kits free.

Details