Dart supports many operators. Here are few important operators:
+
: Addition-
: Subtraction*
: Multiplication/
: Division-expr
: Unary minus, also known as negation (reverse the sign of the expression)~/
: Divide, returning an integer result%
: Get the remainder of an integer division (modulo)++var
: Prefix Increment Operator. For example, var = var + 1 (expression value is var + 1)var++
: Postfix Increment Operator. For example, var = var + 1 (expression value is var)--var
: Prefix Decrement Operator. For example, var = var – 1 (expression value is var – 1)var--
: Postfix Decrement Operator. For example, var = var – 1 (expression value is var)assert(2 + 3 == 5); assert(2 - 3 == -1); assert(2 * 3 == 6); assert(5 / 2 == 2.5); // Result is a double assert(5 ~/ 2 == 2); // Result is an int assert(5 % 2 == 1); // Remainder assert('5/2 = ${5 ~/ 2} r ${5 % 2}' == '5/2 = 2 r 1');
var a, b; a = 0; b = ++a; // Increment a before b gets its value. assert(a == b); // 1 == 1 a = 0; b = a++; // Increment a AFTER b gets its value. assert(a != b); // 1 != 0 a = 0; b = --a; // Decrement a before b gets its value. assert(a == b); // -1 == -1 a = 0; b = a--; // Decrement a AFTER b gets its value. assert(a != b); // -1 != 0
==
: Equal!=
: Not equal>
: Greater than<
: Less than>=
: Greater than or equal to<=
: Less than or equal toThere is only one assignment operator which is =
. You can combine arithmentic operator with the =
to create Compund Assignment Operator
. For example, +=
. Here is the list of few Compund Assignment Operator.
var a = 30; var b = 50; a += b; // a = a + b; print(a); // a = 80
You can invert or combine boolean expressions using the logical operators.
!expr
: inverts the following expression (changes false to true, and vice versa)||
: logical OR&&
: logical ANDif (!done && (col == 0 || col == 3)) { // ...Do something... }
Dart has two operators that let you concisely evaluate expressions that might otherwise require if-else
statements:
condition ? expr1 : expr2
If condition is true
, evaluates expr1
(and returns its value); otherwise, evaluates and returns the value of expr2
.
expr1 ?? expr2
If expr1
is non-null, returns its value; otherwise, evaluates and returns the value of expr2
.
Cascades (..) allow you to make a sequence of operations on the same object. In addition to function calls, you can also access fields on that same object. This often saves you the step of creating a temporary variable and allows you to write more fluid code.
querySelector('#confirm') // Get an object. ..text = 'Confirm' // Use its members. ..classes.add('important') ..onClick.listen((e) => window.alert('Confirmed!'));
The first method call, querySelector()
, returns a selector object. The code that follows the cascade notation operates on this selector object, ignoring any subsequent values that might be returned.
The previous example is equivalent to the following code:
var button = querySelector('#confirm'); button.text = 'Confirm'; button.classes.add('important'); button.onClick.listen((e) => window.alert('Confirmed!'));
Be careful to construct your cascade on a function that returns an actual object. For example, the following code fails:
var sb = StringBuffer(); sb.write('foo') ..write('bar'); // Error: method 'write' isn't defined for 'void'.
The above code doesn't work, as write()
method returns void
, and you can’t construct a cascade on void
.
Like .
, but the leftmost operand can be null
; example: foo?.bar
selects property bar
from expression foo
unless foo
is null
(in which case the value of foo?.bar
is null
)