Flutter: Let’s get familiar with Dart operators

An operator is a special symbol that is used to carry out some specific operation on its operand.

In Dart, we have a rich set of built-in operators to carry out the different types of operations. There are operators for assignment, arithmetic operations, logical operations, and comparison operations, etc.

Operators can be used with many types of variables or constants, but some of the operators are restricted to work on specific data types. Most operators are binary, meaning they take two operands, but a few are unary and only take one operand.

Type of operators in Dart

  • Assignment Operators

  • Arithmetic Operators

  • Type test Operators

  • Relational Operators

  • Logical Operators

  • Bitwise Operators

  • Conditional Operators

  • Casecade notation(..) Operator

I am going to cover those Operators in this story which I am using while wringing flutter code in the last few years with examples.

Assignment Operators

Assignment operators are used to assigning value to a variable, you can assign a variable value or the result of the arithmetical expression. In many cases, the assignment operators can be combined with other operators to build a shorthand version of an assignment statement are known as Compound Statement. For example, instead of a= a+5, we can write a +=5.

Assignment Operators

Arithmetic Operators

Arithmetic Operators are used to performing arithmetic operations like addition, subtraction, multiplication, division, %modulus, exponent, etc.

Let variable a holds 20 and variable and b holds 10, then −

Arithmetic Operators

Unary Operators (post and pre)

In Java, ++ and — are know as increment and decrement operators respectively. These are unary operators it means they work on a single operand. ++ adds 1 to operand and — subtracts 1 to operand respectively. When ++ is used as prefix(like: ++i), ++i will increment the value of i and then return it but, if ++ is used as postfix(like: i++), operator will return the value of operand first and then only increment it.

Unary Operators (post and pre)

Type test Operators

The Type test operators are used for checking types at runtime.

Type test Operators

Relational Operators

Relational Operators are used to evaluating a comparison between two operands. The result of a relational operation is a Boolean value that can only be true or false. Relational Operators are also referred to as Comparison operators.

Let variable a holds 20 and variable and b holds 10, then −

Relational Operators

Logical Operators

Logical operators are used to combining expressions with conditional statements using logical (AND, OR, NOT) operators, which results in true or false.

Let variable a holds 1 and variable and b holds 0, then −

Logical Operators

Bitwise Operators

Bitwise operators are used to performing the bit-level operation over its operand. Let A = 60; and B = 13;

Bitwise Operators

Conditional Operators ( ? : )

The conditional operator is considered as shorthand for the if-else statement. The conditional operator is also called as Ternary Operator.

Conditional Operators ( ? : )

Cascade notation(..) Operator

Cascades (..) allow you to perform a sequence of operations on the same object. The Cascades notation(..) is similar to method chaining that saves you the number of steps and needs for a temporary variable.

Car()
..name = 'Tesla'
..modelX()
..withSolarPanal();

Other operators

  • . Member access — refers to a property of an expression

  • ?. Conditional member access —like the above, but the leftmost operand can be null

class Car {
  String name;
}

void main() {
  Car tesla;
  // prints null
  print(tesla?.name);
  // throws null is not an object exception
  print(tesla.name);
}

#playWithOperatorInDartPad

That’s all you’ll need to get started with dart operators.

0
Subscribe to my newsletter

Read articles from NonStop io Technologies directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

NonStop io Technologies
NonStop io Technologies

Product Development as an Expertise Since 2015 Founded in August 2015, we are a USA-based Bespoke Engineering Studio providing Product Development as an Expertise. With 80+ satisfied clients worldwide, we serve startups and enterprises across San Francisco, Seattle, New York, London, Pune, Bangalore, Tokyo and other prominent technology hubs.