Operators in Dart

Learn via video courses
Topics Covered

Overview

Dart utilizes symbols called operators for various actions on values: arithmetic (+, -, *, /), assignment (=, +=), comparison (==, <, >), logical (&&, ||), and more. Operators perform tasks like calculations, assignments, and condition checks. Unary (++, --) acts on one value, binary (+, ==) on two. Ternary (condition ? expr1 : expr2) offers compact conditionals. Mastery of Dart's operators is vital for efficient coding and app development.

Just like in everyday language, where words enable us to convey thoughts, operators are the language of computations and interactions in programming. Dart provides a rich set of operators that allow you to manipulate values, make decisions, perform calculations, and more. From basic arithmetic to complex logical evaluations, these symbols are the building blocks of code that empower you to create dynamic and functional applications. In this exploration of operators in Dart, you'll uncover how these seemingly simple symbols wield remarkable influence over the behavior and functionality of your code.

CategoryOperatorsDescription
Arithmetic+, -, *, /, %Basic mathematical operations.
Assignment=, +=, -=, *=, /=, %=, ??=Assign values to variables.
Increment/Decrement++, --Increase or decrease variable values by 1.
Comparison==, !=, <, >, <=, >=Compare values for equality, inequality, and order.
Logical&&,
Conditionalcondition ? expr1 : expr2Ternary operator for compact conditionals.
Type Testis, is!, asCheck types and cast objects.
Bitwise&,, ^, ~, <<, >>
Null-aware??, ??=Handle null values with default or assignment.
Membershipin, !inCheck if a value is in a collection.
Cascade..Chain method calls on an object.
Conditionalexpr1 ?? expr2Return expr1 if not null, otherwise expr2.
Function Call()Invoke functions and constructors.
Miscellaneous~/, ~, await, await for, as, show, hideVarious specialized operators for specific tasks.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Different types of operators in Dart

Arithmetic Operators

In Dart, arithmetic operators are used to perform basic mathematical operations on numeric values like integers and floating-point numbers. These operators allow you to perform addition, subtraction, multiplication, division, and other mathematical calculations. Here's an explanation of the arithmetic operators in Dart along with examples of their usage:

  • Addition (+): The addition operator is used to add two numbers together.

    Output

  • Subtraction (-): The subtraction operator is used to subtract one number from another.

    Output

  • Multiplication (*): The multiplication operator is used to multiply two numbers.

    Output

  • Division (/): The division operator is used to divide one number by another. If both operands are integers, the result will also be an integer (truncated division). If at least one operand is a floating-point number, the result will be a floating-point number.

    Output

  • Modulus (%): The modulus operator calculates the remainder when one number is divided by another.

    Output

  • Increment (++) and Decrement (--): The increment operator (++) adds 1 to a variable, and the decrement operator (--) subtracts 1 from a variable.

    Output

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course

Relational Operators

In Dart, relational operators are used to compare values and determine the relationship between them. These operators return a boolean value (true or false) based on whether the comparison is true or false. Here's an explanation of the relational operators in Dart along with examples:

  • Equal to (==): The equality operator checks if two values are equal.

    Output

  • Not equal to (!=): The inequality operator checks if two values are not equal.

    Output

  • Greater than (>): The greater-than operator checks if one value is greater than another.

    Output

  • Less than (<): The less-than operator checks if one value is less than another.

    Output

  • Greater than or equal to (>=): The greater-than-or-equal-to operator checks if one value is greater than or equal to another.

    Output

  • Less than or equal to (<=): The less-than-or-equal-to operator checks if one value is less than or equal to another.

    Output

These relational operators allow you to compare values and make decisions based on their relationships. Keep in mind that the operands of these operators must be of compatible types for meaningful comparisons.

Type Test Operators

In Dart, type test operators are used to check the type of an object or variable. They help you determine whether an object is an instance of a particular class or implements a certain interface. Dart provides two main type test operators: is and as.

  • is: The is operator checks if an object is an instance of a specified type or a subtype of that type. It returns true if the object is of the specified type, otherwise false.

    Output

  • as: The as operator is used to explicitly cast an object to a specific type. If the cast is successful, the expression evaluates to the cast object. If the cast fails (because the object is not of the expected type), a runtime exception occurs.

    Output

Remember that while the is operator helps you check if an object can be safely cast to a type using the as operator, it doesn't guarantee a successful cast. It's a good practice to use type checks and type casts carefully to avoid runtime errors.

Additionally, Dart provides another type test operator, is!, which is the negation of the is operator. It checks if an object is not an instance of a specified type.

Output

These type test operators are useful for runtime type checks and ensuring that your code handles objects of the expected types appropriately.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Bitwise Operator

In Dart, bitwise operators are used to perform operations on individual bits of integer values. These operators are commonly used in low-level programming and situations where you need to manipulate binary representations of data. Dart provides several bitwise operators:

  • Bitwise AND (&): Performs a bitwise AND operation between the corresponding bits of two integer values. The result is 1 only if both bits are 1.

    Output

  • Bitwise OR (|): Performs a bitwise OR operation between the corresponding bits of two integer values. The result is 1 if at least one of the bits is 1.

    Output

  • Bitwise XOR (^): Performs a bitwise XOR (exclusive OR) operation between the corresponding bits of two integer values. The result is 1 if the bits are different.

    Output

  • Bitwise NOT (~): Performs a bitwise NOT operation, which inverts all the bits of an integer value.

    Output

  • Left Shift (<<): Shifts the bits of an integer value to the left by a specified number of positions.

    Output

  • Right Shift (>>): Shifts the bits of an integer value to the right by a specified number of positions. The leftmost bits might be filled with 0 or the sign bit, depending on the sign of the value.

    Output

Assignment Operators

In Dart, assignment operators are used to assign values to variables and can combine an operation with the assignment. These operators help simplify and make code more concise when you want to modify a variable's value based on its current value. Here are the common assignment operators in Dart:

  • Assignment (=): The assignment operator is used to assign a value to a variable.

    Output

  • Add and Assign (+=): This operator adds the right-hand value to the left-hand variable and assigns the result to the left-hand variable.

    Output

  • Subtract and Assign (-=): This operator subtracts the right-hand value from the left-hand variable and assigns the result to the left-hand variable.

    Output

  • Multiply and Assign (*=): This operator multiplies the left-hand variable by the right-hand value and assigns the result to the left-hand variable.

    Output

  • Divide and Assign (/=): This operator divides the left-hand variable by the right-hand value and assigns the result to the left-hand variable.

    Output

  • Modulus and Assign (%=): This operator performs the modulus operation between the left-hand variable and the right-hand value and assigns the result to the left-hand variable.

    Output

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Logical Operators

In Dart, logical operators are used to perform logical operations on boolean values. These operators help you combine multiple conditions and evaluate the resulting boolean value. Dart provides three main logical operators: && (logical AND), || (logical OR), and ! (logical NOT).

  • Logical AND (&&): The logical AND operator returns true if both of its operands are true, otherwise, it returns false.

Output

  • Logical OR (||): The logical OR operator returns true if at least one of its operands is true. It returns false only if both operands are false.

    Output

  • Logical NOT (!): The logical NOT operator negates the value of its operand. If the operand is true, the result is false, and vice versa.

    Output

These logical operators are useful for combining boolean expressions and making decisions based on the truth values of conditions. You can also use parentheses to control the order of evaluation, just like in arithmetic expressions.

Output:

Logical operators are essential for constructing complex conditions and making decisions in your Dart programs.

Conditional Operator

In Dart, the conditional operator, often referred to as the "ternary operator," provides a concise way to write simple conditional expressions. It allows you to evaluate a condition and return one of two values based on whether the condition is true or false. The syntax of the conditional operator is:

Here's how it works:

Output

In this example, if the condition number > 5 is true, the value "Number is greater than 5" is assigned to the result variable. Otherwise, if the condition is false, the value "Number is not greater than 5" is assigned.

You can also nest conditional operators to handle more complex scenarios:

Output

In this nested example, the first condition checks if the age is 18 or older. If true, "You are an adult" is assigned. If not, it checks if the age is at least 13, assigning "You are a teenager" if true, and "You are a child" if neither condition is met.

Cascade Notion Operator

Cascade notation (also known as the cascade operator or cascade notation operator) is a feature in the Dart programming language that allows you to perform a sequence of operations on the same object, without needing to repeatedly reference the object. It is denoted by the double-dot (..) syntax. This operator simplifies and streamlines code, especially when you need to perform multiple method calls or property assignments on the same object.

Sequenced Operations: Cascade notation (..) enables you to chain multiple method calls or property assignments on the same object, one after the other.

Eliminates Repeated Object References: Without cascade notation, you would need to repeatedly reference the same object when performing multiple operations on it. Cascade notation eliminates this repetition, leading to more concise and readable code.

Example Syntax:

Method Calls and Property Assignments: Cascade notation can be used with both method calls and property assignments, allowing you to set properties and call methods on the same object without breaking the chain.

Use Cases:

  • Creating and configuring objects with multiple properties.
  • Calling several methods on an object in sequence.
  • Modifying the same object's properties without repeating its reference.

Cascade notation can be combined with conditional expressions to perform actions on an object conditionally in a concise manner. The cascade notation operator (..) in Dart is a powerful feature that allows you to perform a series of operations on the same object in a concise and readable way, eliminating the need for repeated object references and making your code more efficient.

Conclusion

  • Dart's operators provide fundamental tools for expressing computations and logic succinctly.
  • Dart offers arithmetic, logical, relational, and assignment operators, each serving distinct coding needs.
  • Understanding operator precedence prevents ambiguity and ensures accurate expression evaluation.
  • Unary operators like "!" and "++/--" simplify variable manipulation and modification.
  • Clear differentiation between == (equality) and === (identity) is crucial when comparing values.
  • The ternary operator ("condition ? expr1 : expr2") streamlines simple decision-making.
  • Bitwise operators ("&," "|," "^") allow fine-grained bit manipulation for tasks like encryption.
  • Shorthand assignment operators ("+=" and others) combine assignment with arithmetic operations neatly.