Back to quest
Dart Basics & Data TypesLevel 4 / 32

Variables: var and dynamic

Understanding mutable variables and dynamic typing

In Dart, var and dynamic are two ways to declare variables, but they work very differently. Understanding when to use each is important for writing safe and maintainable code.

The var Keyword

The var keyword lets Dart infer the type of the variable from its initial value. Once assigned, the variable's type is fixed, but you can change its value to another value of the same type.

Using var

example.dart
1var greeting = 'Hello'; // Type: String (inferred)
2greeting = 'Hi'; // ✅ OK - same type
3// greeting = 42; // ❌ Error - different type
4 
5var count = 0; // Type: int (inferred)
6count = 10; // ✅ OK - reassigning with same type
7count = count + 5; // ✅ OK
8 
9// var with explicit initialization
10var items = <String>[]; // Empty list of strings
11items.add('Apple'); // ✅ OK

HintVariables declared with var can be reassigned, but only to values of the same type. The type is inferred from the initial value.

The dynamic Keyword

The dynamic keyword creates a variable that can hold values of any type. The type checking is deferred to runtime, which can be useful but also risky.

Using dynamic

example.dart
1dynamic value = 'Hello'; // Starts as String
2print(value); // Hello
3 
4value = 42; // ✅ OK - can change to int
5print(value); // 42
6 
7value = true; // ✅ OK - can change to bool
8print(value); // true
9 
10value = [1, 2, 3]; // ✅ OK - can change to List
11print(value); // [1, 2, 3]
12 
13// Be careful! No compile-time type checking
14// value.someMethod(); // ⚠️ Runtime error if method doesn't exist

Hintdynamic variables can hold any type and change types at runtime. Use with caution as you lose type safety.

var vs dynamic

The key difference is that var is type-safe (type is fixed after inference), while dynamic is not (type can change).

Comparison

example.dart
1// var - Type is inferred and fixed
2var name = 'Alice'; // Type: String
3// name = 123; // ❌ Error: can't assign int to String
4 
5// dynamic - Type can change
6dynamic data = 'Alice'; // Type: dynamic (currently String)
7data = 123; // ✅ OK: can change to int
8data = true; // ✅ OK: can change to bool
9 
10// Best practice: Prefer var over dynamic
11var score = 100; // ✅ Good: type-safe
12dynamic value = 100; // ⚠️ Use only when necessary

HintUse var for type safety and better code reliability. Use dynamic only when you truly need runtime flexibility.

Loot collected

Key takeaways
  • var infers the type from the initial value and keeps it fixed
  • dynamic allows the variable to change types at runtime
  • var provides compile-time type safety, dynamic does not
  • Prefer var over dynamic for safer, more maintainable code
  • Use dynamic only when you need true runtime type flexibility

Side quests

Practice exercises

1

Exercise 1: Using var

Create a variable using var to store a counter that starts at 0 and can be incremented.

Reveal solution
solution.dart
1var counter = 0; // Type is inferred as int and fixed
2

Exercise 2: When to use dynamic

Give an example of when dynamic might be appropriate.

Reveal solution
solution.dart
1dynamic jsonData = parseJson(); // When working with JSON or external data where type is unknown at compile time
Party chat

Comments