Difference between “var” and “dynamic” type in Dart?

前端 未结 11 2083
攒了一身酷
攒了一身酷 2021-01-30 10:25

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 provide

11条回答
  •  [愿得一人]
    2021-01-30 10:46

    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 ex.

    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.

提交回复
热议问题