Design Converter
Education
Last updated on Oct 26, 2023
Last updated on Oct 3, 2023
Software Development Executive - I
Writes code, blogs, and product docs. She loves a good meal, a great playlist, and a clean commit history. When she’s not debugging, she’s probably experimenting with a new recipe.
Software Development Executive - II
A Flutter and iOS developer.
In Dart, operators are special symbols that represent computations like arithmetic, logical comparisons, and type checks.
In this guide, we will explore the various operators supported in Dart, their usage, and how they can be used to manipulate data and control the flow of your code. We will start with basic arithmetic operators, move on to more advanced logical and bitwise operators, and finally explore some special operators unique to Dart.
Whether you're just starting your journey in Dart or looking to brush up your knowledge, this guide will provide you with a comprehensive understanding of Dart operators. So, let's dive in!
In Dart programming, operators play a crucial role in manipulating data. Let's delve into the basic operators supported by Dart.
Arithmetic operators are used for performing mathematical operations. The following table lists the arithmetic operators supported in Dart.
1 Operator Operator Description 2 + Addition 3 - Subtraction 4 * Multiplication 5 / Division 6
These operators are used for basic mathematical operations. Here's an example:
1 void main() { 2 int a = 10, b = 20; 3 print('Addition: ${a + b}'); 4 print('Subtraction: ${a - b}'); 5 print('Multiplication: ${a * b}'); 6 print('Division: ${a / b}'); 7 } 8
The unary minus operator changes the sign of the expression, while the modulo operator returns the remainder of an integer division.
1 void main() { 2 int a = -10, b = 20; 3 print('Unary Minus: ${-a}'); 4 print('Modulo: ${b % a}'); 5 } 6
A variable's value can be raised or lowered by one using the increment and decrement operators, respectively. Here's an illustration:
1 void main() { 2 int a = 10; 3 a++; 4 print('Increment: $a'); 5 a--; 6 print('Decrement: $a'); 7 } 8
Assignment operators are used to assign values to variables. Dart supports both basic and compound assignment operators.
The basic assignment operator is '='. It assigns the value of the right operand to the left operand.
1 void main() { 2 int a = 10; 3 print('Assigned Value: $a'); 4 } 5
Compound assignment operators combine an operation with an assignment. Here's an example:
1 void main() { 2 int a = 10; 3 a += 20; // a = a + 20 4 print('Compound Assignment: $a'); 5 } 6
Equality and relational operators are used to compare two values. The following table lists these operators.
1 Operator Operator Description 2 == Equal to 3 != Not equal to 4 > Greater than 5 < Less than 6 >= Greater than or equal to 7 <= Less than or equal to 8
Here's an example of using these operators:
1 void main() { 2 int a = 10, b = 20; 3 print('Equal to: ${a == b}'); 4 print('Not Equal to: ${a != b}'); 5 print('Greater than: ${a > b}'); 6 print('Less than: ${a < b}'); 7 print('Greater than or equal to: ${a >= b}'); 8 print('Less than or equal to: ${a <= b}'); 9 } 10
After understanding the basic operators in Dart, let's move on to some of the advanced operators that Dart supports.
Logical operators are used when we want to combine two or more conditions. Dart supports three logical operators: AND, OR, and NOT. Here's the operator description for each:
1 Operator Operator Description 2 && Logical AND 3 || Logical OR 4 ! Logical NOT 5
Here's an example of using these operators:
1 void main() { 2 bool isTrue = true, isFalse = false; 3 print('AND Operator: ${isTrue && isFalse}'); 4 print('OR Operator: ${isTrue || isFalse}'); 5 print('NOT Operator: ${!isTrue}'); 6 } 7
Bitwise operators are used for manipulating the binary representation of values. They operate on the bit level. Here's the operator description for each:
1 Operator Operator Description 2 & Bitwise AND 3 | Bitwise OR 4 ^ Bitwise XOR 5 ~ Bitwise NOT 6 << Left Shift 7 >> Right Shift 8
Here's an example of using these operators:
1 void main() { 2 int a = 2, b = 3; 3 print('Bitwise AND: ${a & b}'); 4 print('Bitwise OR: ${a | b}'); 5 print('Bitwise XOR: ${a ^ b}'); 6 print('Bitwise NOT: ${~a}'); 7 print('Left Shift: ${a << 1}'); 8 print('Right Shift: ${a >> 1}'); 9 } 10
Type test operators are used for checking types at runtime. Dart supports three type test operators: as, is, and is!. Here's the operator description for each:
1 Operator Operator Description 2 as Typecast 3 is True if the object has the specified type 4 is! True if the object doesn’t have the specified type 5
Here's an example of using these operators:
1 void main() { 2 var number = 10; 3 if (number is int) { 4 print('Number is an integer'); 5 } 6 } 7
Conditional expressions in Dart are a way to simplify if-else statements. Dart supports two types of conditional expressions: condition ? expr1 : expr2 and expr1 ?? expr2. Here's the operator description for each:
1Operator Operator Description 2condition ? expr1 : expr2 If condition is true, returns expr1; otherwise, returns expr2 3expr1 ?? expr2 If expr1 is non-null, returns its value; otherwise, returns the value of expr2
Here's an example of using these operators:
1 void main() { 2 var a = 10; 3 var b = a > 5 ? 'a is greater than 5' : 'a is not greater than 5'; 4 print(b); 5 var c = a ?? 'a is null'; 6 print(c); 7 } 8
In addition to the basic and advanced operators, Dart also supports some special operators that can make your code more concise and easier to read.
The cascade notation (..) allows you to perform a sequence of operations on the same object. This can often save you the step of creating a temporary variable and allows you to write more fluid code. Here's an example:
1 void main() { 2 var buffer = StringBuffer() 3 ..write('Hello, ') 4 ..write('World!'); 5 print(buffer.toString()); 6 } 7
In the above example, we are able to call multiple methods on the same object (buffer) in a single statement.
The null assertion operator (!) is used to cast an expression to its underlying non-nullable type, throwing a runtime exception if the cast fails. Here's an example:
1 void main() { 2 int? a = null; 3 try { 4 int b = a!; 5 } catch (e) { 6 print('Exception: $e'); 7 } 8 } 9
In the above example, we are trying to cast a nullable integer to a non-nullable integer, which results in a runtime exception.
Function application (()) and subscript access ([]) are used to call a function and access an element at a specific index in a list, respectively. Here's an example:
1 void main() { 2 var list = [1, 2, 3, 4, 5]; 3 print('Element at index 2: ${list[2]}'); 4 5 void printHello() { 6 print('Hello, World!'); 7 } 8 9 printHello(); 10 } 11
We have now covered the basics of Dart operators, from arithmetic to assignment, logical to bitwise, and even some special operators like cascade notation. We've seen how operators are used to manipulate data, perform operations, and make decisions in our code.
Try to incorporate them into your Dart programming tasks to see how they can make your code more efficient and readable.
We hope this guide has been helpful for you to understand the various operators in Dart.
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.