Difference Between var and dynamic in Dart


Difference Between var and dynamic in Dart

Dart provides several ways to declare variables, but two of the most commonly used keywords are var and dynamic. Although both allow you to create variables without explicitly writing a data type, they behave very differently.

Understanding the difference between these keywords is important for writing clean, maintainable, and type-safe Flutter applications. In this tutorial, we'll explore how var and dynamic work, when to use them, and their advantages and disadvantages with practical
 examples.

difference between var and dynamic in dart



  • What is var in Dart?
The var keyword allows Dart to automatically determine the data type based on the value assigned during initialization.

Once the type is inferred, it becomes fixed and cannot be changed later.

void main() {
  var name = "Sachin";

  name = "Flutter Developer"; // Allowed

  // name = 100; // Error
}

In the above example, Dart infers that name is a String. Therefore, assigning an integer value later will result in a compile-time error.

  • Advantages of var
  • Cleaner and shorter code.
  • Provides compile-time type checking.
  • Better performance compared to dynamic.
  • Helps prevent accidental type-related bugs.

  • What is dynamic in Dart?
The dynamic keyword disables static type checking. A variable declared with dynamic can hold any type of value and can change its type during runtime.

void main() {
  dynamic data = "Flutter";

  data = 100;

  data = true;

  data = [1, 2, 3];

  print(data);
}

Here, the variable data changes from String to Integer, Boolean, and then List without any compile-time error.

  • Advantages of dynamic
  • Useful when data type is unknown.
  • Helpful when working with JSON responses.
  • Can store different types of values in the same variable.

  • var vs dynamic - Practical Example

void main() {

  var age = 25;

  age = 30;

  // age = "Thirty"; // Error


  dynamic value = 25;

  value = "Thirty";

  value = true;

  print(value);
}


  • Key Differences Between var and dynamic
Feature var dynamic
Type Inference Type is inferred at compile time. No fixed type.
Type Safety Type-safe. Not type-safe.
Compile-Time Checking Yes. No.
Can Type Change? No. Yes.
Performance Better. Slightly Lower.
Recommended Usage Most situations. Only when type is unknown.

  • Method Access Example
One important difference appears when accessing methods.

void main() {

  var name = "Flutter";

  print(name.length);


  dynamic value = "Flutter";

  print(value.length);

}

While both examples work, the compiler can verify name.length when using var. With dynamic, validation happens only at runtime.

  • When Should You Use var?
  • When the type is known.
  • For Flutter widgets and business logic.
  • To improve readability and safety.
  • For most application development scenarios.

  • When Should You Use dynamic?
  • When working with APIs returning mixed data.
  • When the data type is truly unknown.
  • For specific runtime scenarios.
  • When flexibility is more important than type safety.

  • Interview Question
Question: What is the difference between var and dynamic in Dart?

Answer: The var keyword infers the type from the assigned value and becomes type-safe at compile time. The dynamic keyword disables static type checking and allows the variable to change its type at runtime.

  • Conclusion
Both var and dynamic are useful in Dart, but they serve different purposes. Use var whenever possible because it provides better type safety, performance, and maintainability. Reserve dynamic for situations where the data type cannot be determined in advance.

As a best practice in Flutter development, prefer var over dynamic unless there is a specific reason to use dynamic.
```