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

前端 未结 11 2084
攒了一身酷
攒了一身酷 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:56

    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 initial value, var is int in this case.

提交回复
热议问题