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
1var greeting = 'Hello'; // Type: String (inferred)2greeting = 'Hi'; // ✅ OK - same type3// greeting = 42; // ❌ Error - different type4 5var count = 0; // Type: int (inferred)6count = 10; // ✅ OK - reassigning with same type7count = count + 5; // ✅ OK8 9// var with explicit initialization10var items = <String>[]; // Empty list of strings11items.add('Apple'); // ✅ OKHintVariables 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
1dynamic value = 'Hello'; // Starts as String2print(value); // Hello3 4value = 42; // ✅ OK - can change to int5print(value); // 426 7value = true; // ✅ OK - can change to bool8print(value); // true9 10value = [1, 2, 3]; // ✅ OK - can change to List11print(value); // [1, 2, 3]12 13// Be careful! No compile-time type checking14// value.someMethod(); // ⚠️ Runtime error if method doesn't existHintdynamic 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
1// var - Type is inferred and fixed2var name = 'Alice'; // Type: String3// name = 123; // ❌ Error: can't assign int to String4 5// dynamic - Type can change6dynamic data = 'Alice'; // Type: dynamic (currently String)7data = 123; // ✅ OK: can change to int8data = true; // ✅ OK: can change to bool9 10// Best practice: Prefer var over dynamic11var score = 100; // ✅ Good: type-safe12dynamic value = 100; // ⚠️ Use only when necessaryHintUse 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
Exercise 1: Using var
Create a variable using var to store a counter that starts at 0 and can be incremented.
Reveal solutionHide solution
1var counter = 0; // Type is inferred as int and fixedExercise 2: When to use dynamic
Give an example of when dynamic might be appropriate.
Reveal solutionHide solution
1dynamic jsonData = parseJson(); // When working with JSON or external data where type is unknown at compile time