HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to DartVariablesData TypesOperatorsStringsListsConditions (if / else)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables & Types
🔢 Numbers & Math
✍️ String
📚 List & Map
🔀 Condition & Loop
🧩 Function
Dart›Variables

Variables

Variable declaration using var, final, and explicit types; constants and reassignment.

Simple enough for a 5-year-old to understand

variable with var

when you write var, Dart infers the type from the value. You can change the value later.

Example code
1
void main() {
2
var name = "Alex";
Text variable (type: String).
VariableText variable (type: String).
3
var age = 36;
Integer variable (type: int).
VariableInteger variable (type: int).
4
print(name);
Alex writes.
Output / InputAlex writes.
5
print(age);
Prints 36.
Output / InputPrints 36.
6
}
VariableOutput / Input
💡 Hint: var = a variable whose type is inferred from the value.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Which one is used for a constant that will not change?

About this topic

There are several ways to define variables in Dart: you can use var for type inference, write the type explicitly, define a name that can be assigned once with final, or define a compile-time known constant with const.

The difference between final and const is subtle but important. final states that the value will be assigned once at runtime; const requires the value to be known while the program is being compiled. A date object can be final but cannot be const.

Null safety directly affects variable declaration. If you do not put a question mark at the end of the type, that variable can never be null and must be assigned a value before it is used. The compiler tracks this.

What you will learn

  • The difference between var, final, and const
  • Type inference and explicit type declaration
  • Nullable types
  • The rule that a variable must be assigned before use
  • Naming conventions

Who is this for?

  • Beginners to Dart
  • Learners of null safety
  • Those struggling to choose between final and const

When to use?

Every value that does not change throughout the flow should be final; in Flutter UI code, this has almost become a rule.

Values that remain constant throughout the program and are known at compile time should be const; const objects are created only once in memory.

Common mistakes

Wrong

final total = 10;
total = 20;

A name defined with final can only receive a value once; a second assignment gives the "The final variable 'total' can only be set once" error. If you are actually going to change the value, you should use var.

Correct

var total = 10;
total = 20;

Wrong

const now = DateTime.now();

const requires the value to be known at compile time; the current time is only known when the program runs, so the "Const variables must be initialized with a constant value" error is thrown. The correct keyword here is final.

Correct

final now = DateTime.now();

Wrong

void main() {
  int count;
  print(count);
}

When null safety is enabled, a non-nullable variable must be assigned before it can be used; the "The non-nullable local variable 'count' must be assigned before it can be used" error is thrown. Either provide an initial value or make the type nullable.

Correct

void main() {
  int count = 0;
  print(count);
}

Frequently asked questions

Should I write var or the type?

If there is an initial value, var is sufficient and shortens the code. Writing the type is useful if you will assign the value later or if the type is ambiguous to the reader. In Flutter projects, var is commonly preferred for local variables and explicit types for fields.

Should I use final or const?

const if the value is known at compile time, final if it is calculated at runtime. const is a stronger guarantee and has memory advantages; const is preferred wherever possible.

What does the late keyword do?

It is for situations where you cannot provide the value of the variable in the declaration but know for sure it will be assigned before first use. This way you don't have to make the type nullable; the risk is that you get a runtime error if it is accessed without being assigned.

Summary

var performs type inference.

final is assigned once, const is known at compile time.

A non-nullable variable must be assigned before use.

A nullable type takes a question mark at the end.

final is preferred for unchanging values.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code