Dart Tutorial

Variables & Types

var, final, const, and type inference in Dart

Strong Typing

Dart is a strongly typed language, but it also has powerful Type Inference. This means you can specify types explicitly, or let the compiler figure them out for you.
dart
var name = 'Alpha'; // Inferred as String
int age = 25;       // Explicitly typed

Immutable Variables

Wait! Choosing between final and const is important in Dart.
  1. final: A variable that can be set only once. Its value is determined at runtime. (e.g., the current time).
  2. const: A variable that is a compile-time constant. Its value must be known before the app runs.
dart
final currentTime = DateTime.now(); // Runtime
const pi = 3.14;                    // Compile-time

String Interpolation

You can put the value of an expression inside a string using the ${} syntax. If it's just a single variable, you can omit the braces.
dart
print('My name is $name and I am $age years old.');

Everything is an Object

In Dart, every variable refers to an Object. There are no "primitives." Even numbers like int and double are objects and can have methods.

Common Data Types

TypeDescription
intInteger values (no decimal)
double64-bit floating-point numbers
numCan be either an int or a double
StringSequence of characters
booltrue or false
dynamicTurns off type checking (Use sparingly!)

Summary

TermDescription
varImplicitly typed variable
finalImmutable after first assignment (Runtime)
constImmutable at compile-time
lateVariable that is initialized after its declaration (Safety check)
dynamicA type that can hold anything (Safety risk!)
Best ForWriting clean and type-safe code
Logic"Immutable by default" makes code easier to reason about
SafetyStrong static analysis catches bugs before you run
ModernConcise syntax with no boilerplate
StandardUse lowerCamelCase for variables
IdentityVariables are references to objects in memory