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

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

    Both in dynamic and var,the variable can hold data of any data type, i.e., int , float,string,etc

    If a variable is declared as a dynamic and if even initialised, its type can change over time.Try this code in https://dartpad.dev/

    void main() {
      dynamic x = 'abc';
      x = 12345;
      print(x);
    
    }
    

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

    void main() {
      var x = 'abc';
      x = 12345;
      print(x);
    }
    

    The above code will result in the error stating that A value of type 'int' can't be assigned to a variable of type 'String' - line 3

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

    void main() {
      var x ;
      x = 'abc';
      x=12345;
      print(x);
    }
    
    0 讨论(0)
  • 2021-01-30 10:35

    dynamic is a type underlying all Dart objects. You shouldn't need to explicitly use it in most cases.

    var is a keyword, meaning "I don't care to notate what the type is here." Dart will replace the var keyword with the initializer type, or leaving it dynamic by default if there is no initializer.

    Use var if you expect a variable assignment to change during its lifetime:

    var msg = "Hello world.";
    msg = "Hello world again.";
    

    Use final if you expect a variable assignment to remain the same during its lifetime:

    final msg = "Hello world.";
    

    Using final (liberally) will help you catch situations where you accidentally change the assignment of a variable when you didn't mean to.

    Note that there is a fine distinction between final and const when it comes to objects. final does not necessarily make the object itself immutable, whereas const does:

    // can add/remove from this list, but cannot assign a new list to fruit.
    final fruit = ["apple", "pear", "orange"];
    fruit.add("grape");
    
    // cannot mutate the list or assign a new list to cars.
    final cars = const ["Honda", "Toyota", "Ford"];
    
    // const requires a constant assignment, whereas final will accept both:
    const names = const ["John", "Jane", "Jack"];
    
    0 讨论(0)
  • 2021-01-30 10:35

    try this in DartPad:

    void main() {
      dynamic x = 'hal';
      x = 123;
      print(x);
      var a = 'hal';
      a = 123;
      print(a);
    }
    

    you can change the type of x, but not a.

    0 讨论(0)
  • 2021-01-30 10:43

    One of aspect than can consider in comparison dynamic vs var is taking into account behavior when using var declaration with initialization at the same time there is not possibility to change type which in case of dynamic is.

    But dynamic vs var is not the question what I would ask. I would ask more what is difference between dynamic vs Object.

    Here is a DO annotate with Object instead of dynamic to indicate any object is allowed.

    It is hard to feel it at the beginning, but dynamic I would relate to generic type argument.

    0 讨论(0)
  • 2021-01-30 10:44

    var, like final, 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:

    String a = "abc"; // type of variable is String
    var a = "abc";    // a simple and equivalent (and also recommended) way
                      // to declare a variable for string types
    

    On the other hand, dynamic is a special type indicating it can be any type (aka class). For example, by casting an object to dynamic, you can invoke any method (assuming there is one).

    (foo as dynamic).whatever(); //valid. compiler won't check if whatever() exists
    (foo as var).whatever(); //illegal. var is not a type
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题