Skip to main content
A typed variable in Dart represents a named storage location bound to a specific data type within a sound static type system. This binding enforces type safety, ensuring that a variable only holds values corresponding to its declared type or a subtype thereof. Dart supports both explicit type annotations and implicit type inference.

Explicit Type Annotation

Explicit typing involves declaring the data type before the variable name. This instructs the compiler to restrict the variable strictly to the defined type. Syntax:
Type variableName = value;
Examples:
int integerValue = 42;
double floatingPoint = 3.14;
String textLiteral = "Hello Dart";
bool isCompiled = true;
List<int> numberList = [1, 2, 3];

Type Inference

Dart allows the omission of the type annotation using the var keyword. The compiler infers the type based on the value assigned during initialization. Once a type is inferred, it is immutable; the variable cannot be reassigned to a value of a different type. Syntax:
var variableName = value;
Behavior:
var inferredString = "Dart"; // Inferred as String
inferredString = "Flutter";  // Valid
// inferredString = 100;     // Compile-time error: A value of type 'int' can't be assigned to a variable of type 'String'.

Dynamic Typing

The dynamic keyword explicitly opts out of static type checking. A variable declared as dynamic can hold a value of any type and can change types during runtime. Syntax:
dynamic variableName = value;
Behavior:
dynamic flexibleVar = "Start as String";
flexibleVar = 100;      // Valid: Type changes to int
flexibleVar = true;     // Valid: Type changes to bool

Nullability (Null Safety)

Dart enforces sound null safety. By default, variables are non-nullable, meaning they cannot contain the value null unless explicitly declared to allow it.

Non-Nullable Types

Variables must be initialized before use and cannot be assigned null.
int nonNullableInt = 10;
// nonNullableInt = null; // Compile-time error

Nullable Types

Appending a question mark (?) to the type declaration creates a nullable type, allowing the variable to hold either a value of that type or null.
int? nullableInt; // Defaults to null
nullableInt = 10; // Valid
nullableInt = null; // Valid

Late Initialization

The late modifier allows the declaration of a non-nullable variable without immediate initialization. It enforces a contract that the variable will be assigned a value before it is accessed. It also enables lazy initialization.
late String deferredValue;

void main() {
  deferredValue = "Initialized later";
  print(deferredValue); // Valid access
}

Immutability

Typed variables can be made immutable using final or const.
  • final: Single-assignment variable initialized at runtime.
  • const: Compile-time constant.
These keywords can replace var or precede the explicit type annotation.
// Type inferred
final runtimeConstant = DateTime.now(); 
const compileTimeConstant = 3.14159;

// Explicitly typed
final int fixedInteger = 10;
const String appName = "DartApp";
Master Dart with Deep Grasping Methodology!Learn More