Difference between Var and Dynamic

There are different types of keywords available in dart by which we can initialize our variables, var and dynamic are from those keywords. Most of the flutter developer use it in their daily life but don’t aware from the difference between them. And they realize it when they go for an interview in a company.
Don’t worry, Today your confusion will be cleared.

var Keyword

If we talk about the var, then var can be used to initial any type of data but once a value of specific data type is assigned to the variable then we can change the value with same data type but can’t it with different data type.

e.g.
I declare a variable ‘a’ with type var and assign it a value ‘0’.

var a = 0;

Now if I want to change the value of ‘a’ then it is possible

a = 1;

But if I want to change the value of ‘a’ with different data type then it is not possible and it will throw an error.

a = “a”;

dynamic Keyword

Usually, we declare our variable with dynamic keyword when we are unaware about the upcoming data from API or any other source.
We can say dynamic is the extended form of var. dynamic has the ability to change the data type of variable with the value.

e.g.
I declare a variable ‘a’ with type dynamic and assign it a value ‘0’.

var a = 0;

if I want to change the value of ‘a’ with different data type then it is possible with dynamic

a = “a”;

Further, you can watch my youtube video on this topic for more clarification.

Hope you will enjoyed this article. Don’t forget to give thumbs up.
Keep Reading!

--

--

Courteous and enthusiastic, Full Stack Developer who have 2.5 years of experience in Flutter/Dart & Python.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Salman Bediya

Courteous and enthusiastic, Full Stack Developer who have 2.5 years of experience in Flutter/Dart & Python.