Site icon TestingDocs.com

Dart Assignment Operators

Dart Assignment Operators

Dart assignment operators are used to initialize and assign values to the operands. The basic assignment operator assigns the value on the right-hand side(RHS) to the variable on the left-hand side(LHS).
The simple assignment operators are as follows:
Operator Symbol Description
          Simple Assignment

The assignment operator is used to assign values to the expression or the variable.

         ??= Null-aware assignment.

This assignment operator assigns the value on the right-hand side to the variable on the left-hand side if the variable is null. Otherwise, the variable’s value remains unchanged.

Example

Let’s look at some example Dart code to understand the operators.

/*
* Assignment Operators demo Dart Program
* Dart Tutorials - www.TestingDocs.com
*/

void main() {
int x=9;
int y=7;

//Assign a value to the variable result
int result= x - y;
print(result);

// Assign a value to a variable when LHS is null
var sum;
sum ??= x+y;

// the sum of x and y is assigned
print(sum);

//Let's try to reassign some value to the variable sum
sum ??= x * y; // value is not assigned

print(sum);
}

 

Compound assignment operators:

Dart also supports compound assignment operators that do some operation on the operands before the assignment.

 

Operator Symbol Name Description
        += Addition assignment Adds the value on the right-hand side to the variable on the left-hand side and assigns the result to the variable.
         -= Subtraction assignment Subtracts the value on the right-hand side from the variable on the left-hand side and assigns the result to the variable.
         *= Multiplication assignment Multiplies the variable on the left-hand side by the value on the right-hand side and assigns the result to the variable.
         /= Division assignment Divides the variable on the left-hand side by the value on the right-hand side and assigns the result to the variable.
         %= Modulus assignment Divides the variable on the left-hand side by the value on the right-hand side and assigns the remainder to the variable.

More information on Dart programming language:

https://dart.dev/

Exit mobile version