Skip to main content
A variable in Dart is a named reference to an object. Since Dart is a purely object-oriented language, every variable refers to an instance of a class, including primitive-like types such as int, double, and bool.

Declaration and Type Inference

Dart is statically typed, meaning variable types are determined at compile-time. Variables can be declared using an explicit type annotation or by allowing the compiler to infer the type. Explicit Typing The type is defined explicitly by the developer.
String name = 'Dart';
Type Inference (var) The var keyword allows the compiler to determine the type based on the initialization.
  • Initialized: If var is used with an immediate value, the type is inferred and fixed.
  • Uninitialized: If var is used without an immediate value, the type resolves to dynamic.
var language = 'Dart'; // Inferred as String. Type is fixed.
var unknown;           // Inferred as dynamic.

Dynamic Typing

To opt out of static type checking, the dynamic keyword is used. Variables declared as dynamic can reference objects of any type, and the type of the reference can change during runtime.
dynamic value = 'Hello';
value = 100; // Allowed

Mutability and Immutability

Variables in Dart are mutable by default unless restricted by specific modifiers.

Mutable Variables

Variables declared with var or an explicit type (without final or const) are mutable. Their references can be reassigned to different objects of the same type.
int count = 0;
count = 1; // Reassignment allowed

Final (Runtime Constant)

The final keyword declares a variable that can only be assigned once. The value can be determined at runtime (e.g., via a function return or constructor execution). Attempting to reassign a final variable results in a compile-time error.
final DateTime now = DateTime.now(); // Set at runtime, immutable thereafter

Const (Compile-time Constant)

The const keyword declares a variable that must be known at compile-time. const variables are implicitly final.
  • Values: Must be hardcoded literals or computed from other compile-time constants.
  • Constructors: A const constructor enables the creation of canonicalized instances. However, the instance is only canonicalized (deduplicated in memory) if it is instantiated using the const keyword.
const double pi = 3.14159; 
var list = const [1, 2, 3]; // Canonicalized list instance

Null Safety

Dart enforces sound null safety. By default, variables are non-nullable, meaning they cannot contain the value null.

Non-nullable

A variable must be initialized with a non-null value before it is used.
int score = 10; // Cannot be null

Nullable

To allow a variable to hold a null reference, append a question mark (?) to the type declaration.
int? score; // Defaults to null
score = 10;
score = null; // Allowed

Late Initialization

The late modifier allows the declaration of a non-nullable variable that is initialized after its declaration but before its first usage. It serves two primary technical functions:
  1. Deferred Initialization: The compiler treats the variable as non-nullable, but runtime checks ensure it is assigned before access.
  2. Lazy Evaluation: If the variable is initialized immediately with late, the initializer expression runs only when the variable is first accessed.
late String description;

void main() {
  description = 'Initialized later';
  print(description); // Safe access
}

Default Values

  • Nullable variables: Uninitialized variables with a nullable type (e.g., int?) have an initial value of null.
  • Non-nullable variables: Must be initialized before use. Dart does not assign default values (like 0 or false) to non-nullable types; failing to initialize them results in a compile-time error.
Master Dart with Deep Grasping Methodology!Learn More