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 Variables


Share

Dart Variables

A variable is a container that stores a value, like a name or a number.

To create a variable, write var, then the name of the variable, then an equals sign = and the value:

var name = 'John';

After you create a variable, you can use its name to get the value:

Example

void main() {
  var name = 'John';
  var age = 30;
  print(name);
  print(age);
}
Try it Yourself »

Result:

John
30

The equals sign is called the assignment operator, because it gives (assigns) a value to the variable.


Variables With a Type

Every value in Dart has a type.

The type tells Dart what kind of value it is, for example text or a whole number.

Instead of var, you can write the type before the variable name:

Example

void main() {
  String name = 'John';
  int age = 30;
  double height = 1.82;
  bool isStudent = false;
  print(name);
  print(age);
  print(height);
  print(isStudent);
}
Try it Yourself »

Result:

John
30
1.82
false

Here are the most common types:

  • String stores text, like 'Hello'
  • int stores whole numbers, like 30 or -5
  • double stores numbers with decimals, like 1.82
  • bool stores true or false

You will learn more about types in the Data Types chapter.



Type Inference

When you use var, Dart looks at the value and finds the type by itself.

This is called type inference.

You can see the type of a value with runtimeType:

Example

void main() {
  var car = 'Volvo';
  var year = 2024;
  var price = 39999.99;
  print(car.runtimeType);
  print(year.runtimeType);
  print(price.runtimeType);
}
Try it Yourself »

Result:

String
int
double

Note: Both styles are correct.

Many Dart developers use var for local variables, because the type is easy to see from the value.


Change the Value

You can give a variable a new value later in the program:

Example

void main() {
  var score = 10;
  print(score);
  score = 25;
  print(score);
}
Try it Yourself »

Result:

10
25

Note that you do not write var again when you change the value.

A Variable Cannot Change Type

The new value must have the same type as the old value.

The variable score below is an int, so you cannot put text in it:

Example

void main() {
  var score = 10;
  score = 'ten';
  print(score);
}
Try it Yourself »

Result:

main.dart:3:11: Error: A value of type 'String' can't be assigned to a variable of type 'int'.
  score = 'ten';
          ^

Dart finds this mistake before the program runs, so you can fix it early.


Create Now, Assign Later

You can create a variable with a type and give it a value later:

Example

void main() {
  String city;
  city = 'Oslo';
  print(city);
}
Try it Yourself »

Result:

Oslo

But you must give the variable a value before you use it.

If you do not, the program does not run:

Example

void main() {
  String city;
  print(city);
}
Try it Yourself »

Result:

main.dart:3:9: Error: Non-nullable variable 'city' must be assigned before it can be used.
  print(city);
        ^^^^

This rule is part of null safety, which you will learn about in the Null Safety chapter.


Variable Names

A variable name is also called an identifier.

These are the rules for variable names in Dart:

  • Names can contain letters, digits, underscores _ and dollar signs $
  • Names cannot start with a digit
  • Names cannot contain spaces
  • Names are case-sensitive, so age and Age are two different variables
  • Names cannot be reserved words, like var, class or if

Dart developers write variable names in camelCase.

The first word starts with a small letter, and every word after it starts with a capital letter:

Example

void main() {
  var firstName = 'Anna';
  var totalPrice = 120;
  var isLoggedIn = true;
  print(firstName);
  print(totalPrice);
  print(isLoggedIn);
}
Try it Yourself »

Result:

Anna
120
true

A reserved word cannot be used as a name:

Example

void main() {
  var class = 'Math';
}
Try it Yourself »

Result:

main.dart:2:7: Error: 'class' can't be used as an identifier because it's a keyword.
Try renaming this to be an identifier that isn't a keyword.
  var class = 'Math';
      ^^^^^


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.