FLUTTER INTERVIEW QUESTIONS

What’s the difference between var and dynamic type in Dart?

Jelena Lecic
2 min readDec 18, 2019

--

If a variable is declared as a dynamic, its type can change over time.

dynamic a = 'abc'; //initially it's a string
a = 123; //then we assign an int value to it
a = true; //and then a bool

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

var b = 'cde'; //b is a string, and its type can not change
b = 123; //this will not compile
//can not assign an int to the string variable

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

var a; //this is actually a dynamic type
a = 2; //assigned an int to it
a = 'hello!'; //assigned a string to it
print(a); //prints out 'hello'

… and if you liked this article, please clap few times :)

--

--