Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Variables in Dart | Variables and Data Types in Dart
Introduction to Dart

bookVariables in Dart

A variable is a named space in memory that stores values. In other words, it acts as a container for values in a program. A variable must be declared before use. Dart uses the var keyword for this purpose. The syntax for declaring a variable is shown below.

main.dart

main.dart

copy
123
void main() { var name = 'Alex'; // Variable with string value }

A variable in Dart consists of three components: type (e.g., var), name (e.g., name), and value (e.g., 'Alex'). The type defines the data type, the name is the identifier used to reference the variable, and the value is the actual data stored in the variable.

Variable Declaration Rules

  • Variables name must be unique within a block of code;

  • Variables name can't be the keyword (like var or void);

  • Variables name should not start with a number;

  • Variables name must not contain spaces or special symbols, except the underscore(_) and the dollar($) sign.

In this case, you work with the value stored inside the variable:

main.dart

main.dart

copy
1234
void main() { var name = 'Bob'; print(name); }

You can write the numbers calculation result or join strings into a variable:

main.dart

main.dart

copy
1234
void main() { var age = 27 + 3; print(age); }

Keyword Var

When declaring a variable using var, we do not explicitly indicate the type of data that will be stored in this variable. Dart automatically determines what data type a variable will contain when it is initialized.

main.dart

main.dart

copy
123
void main() { var age = 27; }

With such initialization, the age variable will store integer values.

Please note that after initializing a variable with one data type, you cannot reassign the data type for the variable!

main.dart

main.dart

copy
1234
void main() { var age = 10; age = 'ten'; // Error }

You have successfully written the value 28 to the variable age but cannot write the String value 28 to this variable.

Type-Checking in Dart

Type-checking in Dart ensures that the data types in your code match the expected types. It helps prevent errors caused by assigning values of the wrong type to variables.

Note
Study More

Dart uses static typing, meaning the compiler checks types before running the code. It provides more safety than dynamic typing, which is used in JavaScript.

Type Annotations

To improve code readability and structure, use type annotations. Instead of the var keyword, specify the variable’s data type.

Below is a syntax comparison of type annotation and the var keyword.

Type annotations are optional in Dart since the Dart can usually determine the type of a variable by its initial value. However, using type annotations is recommended as it can help prevent errors and make your code more readable and understandable.

question-icon

Define a variable.

Click or drag`n`drop items and fill in the blanks

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 2

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain more about how type-checking works in Dart?

What happens if I try to assign a value of a different type to a variable?

When should I use type annotations instead of the var keyword?

Awesome!

Completion rate improved to 4.55

bookVariables in Dart

Swipe to show menu

A variable is a named space in memory that stores values. In other words, it acts as a container for values in a program. A variable must be declared before use. Dart uses the var keyword for this purpose. The syntax for declaring a variable is shown below.

main.dart

main.dart

copy
123
void main() { var name = 'Alex'; // Variable with string value }

A variable in Dart consists of three components: type (e.g., var), name (e.g., name), and value (e.g., 'Alex'). The type defines the data type, the name is the identifier used to reference the variable, and the value is the actual data stored in the variable.

Variable Declaration Rules

  • Variables name must be unique within a block of code;

  • Variables name can't be the keyword (like var or void);

  • Variables name should not start with a number;

  • Variables name must not contain spaces or special symbols, except the underscore(_) and the dollar($) sign.

In this case, you work with the value stored inside the variable:

main.dart

main.dart

copy
1234
void main() { var name = 'Bob'; print(name); }

You can write the numbers calculation result or join strings into a variable:

main.dart

main.dart

copy
1234
void main() { var age = 27 + 3; print(age); }

Keyword Var

When declaring a variable using var, we do not explicitly indicate the type of data that will be stored in this variable. Dart automatically determines what data type a variable will contain when it is initialized.

main.dart

main.dart

copy
123
void main() { var age = 27; }

With such initialization, the age variable will store integer values.

Please note that after initializing a variable with one data type, you cannot reassign the data type for the variable!

main.dart

main.dart

copy
1234
void main() { var age = 10; age = 'ten'; // Error }

You have successfully written the value 28 to the variable age but cannot write the String value 28 to this variable.

Type-Checking in Dart

Type-checking in Dart ensures that the data types in your code match the expected types. It helps prevent errors caused by assigning values of the wrong type to variables.

Note
Study More

Dart uses static typing, meaning the compiler checks types before running the code. It provides more safety than dynamic typing, which is used in JavaScript.

Type Annotations

To improve code readability and structure, use type annotations. Instead of the var keyword, specify the variable’s data type.

Below is a syntax comparison of type annotation and the var keyword.

Type annotations are optional in Dart since the Dart can usually determine the type of a variable by its initial value. However, using type annotations is recommended as it can help prevent errors and make your code more readable and understandable.

question-icon

Define a variable.

Click or drag`n`drop items and fill in the blanks

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 2
some-alt