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

bookIntroduction to Variables in Dart

Programming relies on the ability to store, retrieve, and manipulate information. This is where variables come in. A variable is a named space in your program's memory where you can store data, such as numbers, text, or other types of information. Variables allow you to give meaningful names to the data your program uses, making your code easier to read, understand, and update. By using variables, you can write programs that remember user input, track scores in a game, or store configuration settings. In Dart, variables are fundamental building blocks that let you work with information in a flexible and organized way.

main.dart

main.dart

copy
1234
void main() { var message = 'Hello, Dart!'; print(message); }

In the Dart code above, you see the declaration of a variable using the var keyword. The statement var message = 'Hello, Dart!'; creates a variable named message and assigns it the value 'Hello, Dart!'. Dart automatically figures out that message should be a string based on the value you assign. The print(message); line then outputs the value of message to the console. This simple example demonstrates how to declare a variable, assign a value, and display it in Dart.

main.dart

main.dart

copy
12345678910111213
void main() { var name = 'Alice'; final age = 30; const country = 'USA'; print(name); // Output: Alice print(age); // Output: 30 print(country); // Output: USA name = 'Bob'; // This is allowed // age = 31; // Uncommenting this line will cause an error // country = 'Canada'; // Uncommenting this line will cause an error }

Dart provides several ways to declare variables, each with different rules about whether the value can change. When you use var, you create a variable whose value can change during the program. The final keyword creates a variable that can only be set once—you cannot change its value after it is assigned. The const keyword creates a compile-time constant, meaning its value must be known at compile time and can never change. Trying to reassign a value to a final or const variable will result in an error. For example:

final score = 100;
score = 200; // Error: a final variable can only be set once.

Use final when you want to assign a value once at runtime, and const when the value is fixed and known at compile time. This distinction helps you write safer, more predictable code.

Note
Definition

Variable: a named storage location in memory for holding a value that can be used and modified in a program.

Immutable: describes a value or variable that cannot be changed after it is set.

Constant: in Dart, a value marked with const that is known at compile time and cannot be changed.

1. What is the main difference between final and const in Dart?

2. Which keyword allows you to declare a variable whose value can change?

3. Why might you use const instead of final?

question mark

What is the main difference between final and const in Dart?

Select the correct answer

question mark

Which keyword allows you to declare a variable whose value can change?

Select the correct answer

question mark

Why might you use const instead of final?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 1

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

bookIntroduction to Variables in Dart

Swipe um das Menü anzuzeigen

Programming relies on the ability to store, retrieve, and manipulate information. This is where variables come in. A variable is a named space in your program's memory where you can store data, such as numbers, text, or other types of information. Variables allow you to give meaningful names to the data your program uses, making your code easier to read, understand, and update. By using variables, you can write programs that remember user input, track scores in a game, or store configuration settings. In Dart, variables are fundamental building blocks that let you work with information in a flexible and organized way.

main.dart

main.dart

copy
1234
void main() { var message = 'Hello, Dart!'; print(message); }

In the Dart code above, you see the declaration of a variable using the var keyword. The statement var message = 'Hello, Dart!'; creates a variable named message and assigns it the value 'Hello, Dart!'. Dart automatically figures out that message should be a string based on the value you assign. The print(message); line then outputs the value of message to the console. This simple example demonstrates how to declare a variable, assign a value, and display it in Dart.

main.dart

main.dart

copy
12345678910111213
void main() { var name = 'Alice'; final age = 30; const country = 'USA'; print(name); // Output: Alice print(age); // Output: 30 print(country); // Output: USA name = 'Bob'; // This is allowed // age = 31; // Uncommenting this line will cause an error // country = 'Canada'; // Uncommenting this line will cause an error }

Dart provides several ways to declare variables, each with different rules about whether the value can change. When you use var, you create a variable whose value can change during the program. The final keyword creates a variable that can only be set once—you cannot change its value after it is assigned. The const keyword creates a compile-time constant, meaning its value must be known at compile time and can never change. Trying to reassign a value to a final or const variable will result in an error. For example:

final score = 100;
score = 200; // Error: a final variable can only be set once.

Use final when you want to assign a value once at runtime, and const when the value is fixed and known at compile time. This distinction helps you write safer, more predictable code.

Note
Definition

Variable: a named storage location in memory for holding a value that can be used and modified in a program.

Immutable: describes a value or variable that cannot be changed after it is set.

Constant: in Dart, a value marked with const that is known at compile time and cannot be changed.

1. What is the main difference between final and const in Dart?

2. Which keyword allows you to declare a variable whose value can change?

3. Why might you use const instead of final?

question mark

What is the main difference between final and const in Dart?

Select the correct answer

question mark

Which keyword allows you to declare a variable whose value can change?

Select the correct answer

question mark

Why might you use const instead of final?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 1
some-alt