According to this article:
As you might know,
dynamic
(as it is now called) is the stand-in type when a static type annotation is not provided.
So, what is the difference between dynamic
and var
? When to use?
According to this article:
As you might know,
dynamic
(as it is now called) is the stand-in type when a static type annotation is not provided.
So, what is the difference between dynamic
and var
? When to use?
var
, likefinal
, 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:
On the other hand,
dynamic
is a special type indicating it can be any type (aka class). For example, by casting an object todynamic
, you can invoke any method (assuming there is one).A
dynamic
variable can change his type and avar
type can't be changed.For example :