What’s the Difference Between var and dynamic type in Dart ??

· 6 min read
What's the Difference Between var and dynamic type in Dart
What's the Difference Between var and dynamic type in Dart

A dynamic variable can change its type and a var type can’t be changed. dynamic is a type underlying all Dart objects. So in this article, we will go through What’s the Difference Between var and dynamic type in Dart.

What’s the Difference Between var and dynamic type in Dart ??

Use var if you expect a variable assignment to change during its lifetime:

var msg = "Hello world.";
msg = "Hello world again.";

Use final if you expect a variable assignment to remain the same during its lifetime:

final msg = "Hello world.";

Using final (literally) will help you catch situations where you accidentally change the assignment of a variable when you didn’t mean to.

Note: that there is a fine distinction between final and const when it comes to objects. final does not necessarily make the object itself immutable, whereas const does:

// can add/remove from this list, but cannot assign a new list to fruit.
final fruit = ["apple", "pear", "orange"];
fruit.add("grape");

// cannot mutate the list or assign a new list to cars.
final cars = const ["Honda", "Toyota", "Ford"];

// const requires a constant assignment, whereas final will accept both:
const names = const ["John", "Jane", "Jack"];

var, like final, is used to declare a variable. It is not a type at all.

Dart is smart enough to know the exact type in most situations. For example, the following two statements are equivalent

String a = "abc"; // type of variable is String
var a = "abc";    // a simple and equivalent (and also recommended) way
                  // to declare a variable for string types

On the other hand, the dynamic is a special type indicating it can be any type (aka class).

For example, by casting an object to dynamic, you can invoke any method (assuming there is one).

(foo as dynamic).whatever(); //valid. compiler won't check if whatever() exists
(foo as var).whatever(); //illegal. var is not a type

Consider a code snippet like the below:

void main() {
  dynamic x = 'hal';
  x = 123;
  print(x);
  var a = 'hal';
  a = 123;
  print(a);
}

you can change the type of x, but not a. Consider a code snippet like the below:

var a ;
a = 123;
print(a is int);
print(a);
a = 'hal';
print(a is String);
When defined without initial value, var is dynamic
var b = 321;
print(b is int);
print(b);
//b = 'hal'; //error
print(b is String);

When defined with an initial value, var is int in this case.

  • dynamic: can change TYPE of the variable, & can change VALUE of the variable later in code.
  • var: can’t change TYPE of the variable, but can change the VALUE of the variable later in code.
  • final: can’t change TYPE of the variable, & can’t change VALUE of the variable later in code.

For example :

var myVar = 'hello';
dynamic myDynamicVar = 'hello';
myVar = 123; // not possible
myDynamicVar = 123; // possible

Both in dynamic and var, the variable can hold data of any data type, i.e., int, float, string, if a variable is declared as a dynamic and if even initialized, its type can change over time.

void main() {
  dynamic x = 'abc';
  x = 12345;
  print(x);

}

If you declare a variable as a var, once assigned type can not change.

void main() {
  var x = 'abc';
  x = 12345;
  print(x);
}

The above code will result in the error stating that A value of type ‘int‘ can’t be assigned to a variable of type ‘String‘ – line 3

BUT, if you state a var without initializing, it becomes a dynamic:

void main() {
  var x ;
  x = 'abc';
  x=12345;
  print(x);
}

If you use var you can’t change the data type of the variable. But if you use dynamic you can change it freely.

For example:

dynamic x = 12; // type: integer
x= "Hello world"; // type: string

This will work with no issues if you do the same using var instead of dynamic you will get an error since you can’t change the data type because it is automatically assigned to the variable when initialized.

Conclusion:

In this article, we have been through what’s the Difference Between var and dynamic type in Dart 

Keep Learning !!! Keep Fluttering !!!

Do let us know in the comments if you are still confused in flutter!!

FlutterAgency.com is our portal Platform dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget GuideFlutter ProjectsCode libs and etc.

FlutterAgency.com is one of the most popular online portals dedicated to Flutter Technology and daily thousands of unique visitors come to this portal to enhance their knowledge of Flutter.

Leave a Reply