Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

Dart Tutorial

Dart HOME Dart Intro Dart Get Started Dart Syntax Dart Output Dart Comments Dart Variables Dart final and const Dart Data Types Dart Numbers Dart Strings Dart String Interpolation Dart String Methods Dart Booleans Dart Type Conversion Dart Null Safety Dart late Dart Operators Dart Arithmetic Dart Assignment Dart Comparison Dart Logical Dart Type Test Dart Null Operators Dart Cascade Dart If...Else Dart Switch Dart While Loop Dart For Loop Dart Break/Continue

Dart Collections

Dart Lists Dart Access List Items Dart Change List Items Dart Loop Lists Dart List Methods Dart Sets Dart Maps Dart Access Map Items Dart Loop Maps Dart Collection if/for Dart Spread Operator

Dart Functions

Dart Functions Dart Parameters Dart Named Parameters Dart Return Values Dart Arrow Functions Dart Anonymous Functions Dart Closures Dart Scope Dart Recursion

Dart Classes

Dart OOP Dart Classes/Objects Dart Constructors Dart Named Constructors Dart Factory Constructors Dart Getters and Setters Dart Static Members Dart Inheritance Dart Abstract Classes Dart Interfaces Dart Mixins Dart Extension Methods Dart Enums Dart Generics

Dart 3 Features

Dart Records Dart Patterns Dart Switch Expressions Dart Sealed Classes Dart Class Modifiers

Dart Errors

Dart Exceptions Dart Custom Exceptions

Dart Async

Dart Async Intro Dart Futures Dart async/await Dart Streams Dart Isolates

Dart Libraries

Dart Imports Dart Packages Dart Math Dart Dates Dart JSON Dart RegExp Dart User Input Dart Files Dart Testing

Dart Examples

Dart Examples Dart Exercises


Dart Assignment Operators


Share

Dart Assignment Operators

Assignment operators are used to give values to variables.

The most common one is =, which gives the value on the right to the variable on the left:

Example

void main() {
  int score = 10;
  print(score);

  score = 25;
  print(score);
}
Try it Yourself »

Result:

10
25

Compound Assignment

A compound assignment operator does a calculation and an assignment in one step.

x += 5 is a short way to write x = x + 5:

Example

void main() {
  int points = 10;
  points += 5;
  print(points);
}
Try it Yourself »

Result:

15

A list of all assignment operators:

OperatorExampleSame as
=x = 5x = 5
+=x += 3x = x + 3
-=x -= 3x = x - 3
*=x *= 3x = x * 3
/=x /= 3x = x / 3
~/=x ~/= 3x = x ~/ 3
%=x %= 3x = x % 3
??=x ??= 3x = x ?? 3

Example

void main() {
  int x = 20;

  x -= 4;
  print(x);

  x *= 2;
  print(x);

  x ~/= 5;
  print(x);

  x %= 4;
  print(x);
}
Try it Yourself »

Result:

16
32
6
2


Assignment with Strings

The += operator also works with strings.

It adds text to the end of a string:

Example

void main() {
  String greeting = 'Hello';
  greeting += ' World';
  print(greeting);
}
Try it Yourself »

Result:

Hello World

The /= Operator and int

The / operator always gives a double.

So you cannot use /= on an int variable:

Example

void main() {
  int total = 10;
  total /= 2;
  print(total);
}
Try it Yourself »

Result:

main.dart:3:9: Error: A value of type 'double' can't be assigned to a variable of type 'int'.
  total /= 2;
        ^

Use a double variable with /=, or use ~/= for integer division:

Example

void main() {
  double price = 10;
  price /= 4;
  print(price);

  int total = 10;
  total ~/= 4;
  print(total);
}
Try it Yourself »

Result:

2.5
2

The ??= Operator

The ??= operator gives a value to a variable only if the variable is null:

Example

String? color;

void main() {
  color ??= 'blue';
  print(color);

  color ??= 'red';
  print(color);
}
Try it Yourself »

Result:

blue
blue

The second ??= does not change color, because it already has a value.

You can read more in the Null-aware Operators chapter.



Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.