Operators are the symbols that allow you to perform calculations, compare values, and make decisions in your code.
1. Arithmetic Operators
Beyond basic math (+, -, *, /), Dart includes specific operators for integer division, modulo, and incrementing.
Math & Increment
1int a = 10;2int b = 3;3 4// Integer Division (Trims decimals)5print(a ~/ b); // Output: 36 7// Modulo (Remainder)8print(a % b); // Output: 19 10// Increment (++) and Decrement (--)11int x = 0;12x++; // x is now 113x--; // x is now 014 15// Prefix vs Postfix16int y = 5;17print(y++); // Prints 5, THEN changes y to 6 (Postfix)18print(++y); // Changes y to 7, THEN prints 7 (Prefix)HintThe distinction between ++i (prefix) and i++ (postfix) is subtle but important in loop logic.
2. Type Test Operators (is, as)
Dart allows you to check types at runtime using is and is!, or cast types using as.
Checking Types
1var value = 'Hello World';2 3if (value is String) {4 print('It is a string with length ${value.length}');5}6 7if (value is! int) {8 print('It is definitely not a number');9}10 11// Type Casting (Use carefully!)12dynamic random = 'Text';13String text = random as String; // Explicitly casting to StringHintRuntime type checking is crucial when dealing with dynamic data or API responses.
3. Assignment Operators
You can combine math and assignment (+=, -=) or use the powerful null-aware assignment (??=).
Compound Assignment
1double score = 10.0;2score += 5; // Same as: score = score + 5;3score *= 2; // Same as: score = score * 2;4 5// Null-aware Assignment (??=)6// Only assigns value if the variable is currently null7String? name; // null by default8name ??= 'Guest'; 9print(name); // Guest10 11name ??= 'Alice';12print(name); // Still Guest (was not null)Hint??= is great for setting default values for variables that might be uninitialized.
4. Logical & Relational Operators
Used for boolean logic. Dart uses Short-Circuit Evaluation for && and ||.
Short-Circuit Logic
1bool isGood = true;2bool isCheap = false;3 4// AND (&&): Both must be true5// If left side is false, it doesn't bother checking the right side.6if (isGood && isCheap) {7 print('Buy it!');8}9 10// OR (||): One must be true11// If left side is true, it skips checking the right side.12if (isGood || isCheap) {13 print('Consider it.');14}HintShort-circuiting improves performance and prevents errors (e.g., checking if an object is null before accessing a property).
5. Conditional Expressions
Dart provides concise ways to write if/else logic using ? : and ??.
Ternary & Null-Coalescing
1int age = 20;2 3// Ternary Operator (condition ? ifTrue : ifFalse)4String status = age >= 18 ? 'Adult' : 'Minor';5 6// Null-Coalescing Operator (??)7// Returns the left expression if visible, otherwise the right8String? userInput;9String username = userInput ?? 'Anonymous'; 10 11print(username); // AnonymousHintThe Ternary operator is cleaner than a 5-line if-else block for variable assignment. ?? is essential for handling nullable values safely.
0 / 5 cleared
Practice Exercises
Test your understanding of Dart operators
Challenge 1
Write code that checks if a number is even using the modulo operator.
1int number = 42;2// Your code hereChallenge 2
What is the key difference between `++x` and `x++`?
Challenge 3
Use the null-aware operator (??) to provide a default value of "Guest" if name is null.
1String? name = null;2String displayName = // Your code hereChallenge 4
Use the ternary operator to assign "Pass" if score >= 60, otherwise "Fail".
1int score = 75;2String result = // Your code hereChallenge 5
What does short-circuit evaluation mean for the && operator?
Loot collected
Key takeaways- Use
~/for integer division. %gives the remainder of a division.==checks equality,!=checks inequality.&&means AND,||means OR,!means NOT.