Dart Operators and Strings
Operators are the small symbols that calculate, compare, and combine values. Strings are the pieces of text that every app displays, from usernames to error messages. Together they form the toolkit behind almost every line of Dart you write.
Arithmetic Operators
| Symbol | Meaning | Example | Result |
|---|---|---|---|
| + | Add | 7 + 2 | 9 |
| - | Subtract | 7 - 2 | 5 |
| * | Multiply | 7 * 2 | 14 |
| / | Divide (decimal result) | 7 / 2 | 3.5 |
| ~/ | Divide (whole number result) | 7 ~/ 2 | 3 |
| % | Remainder | 7 % 2 | 1 |
Dart always returns a decimal from /, even when the numbers divide evenly. Use ~/ when you need a whole number, such as the count of full pages in a book.
Increment and Assignment
int score = 10;
score++; // 11
score--; // 10
score += 5; // 15
score -= 3; // 12
score *= 2; // 24
Compound assignments such as += shorten code that updates a variable using its own value.
Comparison Operators
Comparisons produce a bool, a value that is either true or false.
int age = 20;
print(age == 20); // true (equal)
print(age != 18); // true (not equal)
print(age > 18); // true
print(age <= 20); // true
Logical Operators
Logical operators combine several conditions into one decision.
| Symbol | Name | True When |
|---|---|---|
| && | AND | Both sides are true |
| || | OR | At least one side is true |
| ! | NOT | The value is false |
bool hasTicket = true;
bool isAdult = false;
if (hasTicket && isAdult) {
print('Enter');
} else {
print('Stop'); // this line runs
}
hasTicket isAdult hasTicket && isAdult
true true true
true false false
false true false
false false false
The Ternary Operator
The ternary operator picks one of two values in a single line. Read it as a question: condition, then answer for yes, then answer for no.
int marks = 72;
String result = marks >= 40 ? 'Pass' : 'Fail';
Flutter code uses it constantly inside widgets, such as choosing between two icons.
The Cascade Operator
Two dots in a row call several methods on the same object without repeating its name.
var list = []
..add('Apple')
..add('Banana')
..add('Cherry');
Creating Strings
Single or double quotes both work. Pick one style and keep it consistent across the project.
String a = 'Hello';
String b = "World";
String poem = '''Line one
Line two
Line three''';
Triple quotes create text that spans several lines. A raw string starts with the letter r and ignores escape characters, which helps with file paths and patterns.
String path = r'C:\new\table';
String Interpolation
A dollar sign inserts a variable into text. Curly braces insert a full expression.
String name = 'Asha';
int items = 3;
print('Hello, $name');
print('Total: ${items * 50} rupees');
Interpolation reads better than joining pieces with a plus sign.
Useful String Methods
| Method | Example | Result |
|---|---|---|
| length | 'Flutter'.length | 7 |
| toUpperCase() | 'dart'.toUpperCase() | DART |
| trim() | ' hi '.trim() | hi |
| contains() | 'Flutter'.contains('utt') | true |
| substring() | 'Flutter'.substring(0, 3) | Flu |
| replaceAll() | 'a-b-c'.replaceAll('-', '/') | a/b/c |
| split() | 'a,b,c'.split(',') | [a, b, c] |
| padLeft() | '7'.padLeft(3, '0') | 007 |
Strings in Dart never change after creation. Every method returns a new string and leaves the original untouched.
Building Long Text with StringBuffer
Joining text inside a loop creates many temporary strings. A StringBuffer collects pieces efficiently and produces the final text once.
var buffer = StringBuffer();
for (int i = 1; i <= 3; i++) {
buffer.write('Row $i\n');
}
print(buffer.toString());
Converting Between Text and Numbers
int a = int.parse('42');
double b = double.parse('3.14');
int? c = int.tryParse('abc'); // null instead of a crash
String d = 99.toString();
String e = 3.14159.toStringAsFixed(2); // 3.14
Use tryParse for text typed by users. A mistyped value then produces null instead of an error.
Practice Ideas
Write a program that takes a full name, trims extra spaces, and prints the initials in capital letters. Build a bill calculator that adds tax with arithmetic operators and prints the total using interpolation and two decimal places.
Operator Order
Dart follows the same order of operations as mathematics: multiplication and division run before addition and subtraction, and parentheses override everything. The expression 2 + 3 * 4 equals 14, while (2 + 3) * 4 equals 20. Add parentheses whenever a line mixes several operators. The extra symbols cost nothing and spare readers from guessing.
