Zum Inhalt springen
KodeTrail

Camp 1 · Schritt 3 von 12

Variables and final

var, final, and explicit types — how Dart stores values and why 'final by default' is the local dialect.

12 Min.+50 XPAuf Englisch angezeigt — Übersetzung ist unterwegs

Dart gives you three ways to declare a variable, each saying something different to the reader.

var: inferred and changeable

DartCloud-Run

var infers the type from the value — and the type then sticks: steps = 'many'; would be a compile error, because steps is an int forever. Inferred ≠ loose.

final: set once

DartCloud-Run

A final variable is assigned exactly once. Most values in real apps never change after creation, so idiomatic Dart (and Flutter) code reads final first, var only when mutation is needed — same wisdom as JavaScript's const-first habit.

Explicit types

You can always write the type yourself instead of var:

int lessons = 12;
double km = 7.5;
String name = 'Ada';
bool done = false;

Style guide summary: let inference work (var/final) inside functions; write explicit types on function signatures. You'll see both everywhere.

Checkpoint

After var count = 3; what does count = 'three'; do?

Checkpoint

Which declaration fits a value assigned once and never changed?