Difference between “var” and “dynamic” type in Dar

2020-05-15 08:29发布

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?

标签: dart
8条回答
爷的心禁止访问
2楼-- · 2020-05-15 09:05
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.

查看更多
走好不送
3楼-- · 2020-05-15 09:07

To clarify some of the previous answers, when you're declaring a variable as dynamic, it's type changes depending on what you assign to it. When you're declaring a var, the type is set once it's assigned something, and it cannot be changed after that.

For example, the following code:

dynamic foo = 'foo';
print('foo is ${foo.runtimeType} ($foo)');
foo = 123;
print('foo is ${foo.runtimeType} ($foo)');

will return the following result when run in DartPad:

foo is String (foo)
foo is int (123)

But the following code won't even compile:

var bar = 'bar';
print('bar is ${bar.runtimeType} ($bar)');
bar = 123; // <-- Won't compile, because bar is a String
print('bar is ${bar.runtimeType} ($bar)');

Long story short - use dynamic if you want a non-typed variable, use var when you want a typed variable with whatever type you assign to it.

查看更多
男人必须洒脱
4楼-- · 2020-05-15 09:09

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.

查看更多
▲ chillily
5楼-- · 2020-05-15 09:11

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"];
查看更多
\"骚年 ilove
6楼-- · 2020-05-15 09:13

dynamic: can change TYPE of the variable, & can change VALUE of the variable later in code.

var: can't change TYPE of the variable, but can change VALUE of the variable later in code.

final: can't change TYPE of the variable, & can't change VALUE of the variable later in code.

dynamic v = 123;   // v is of type int.
v = 456;           // changing value of v from 123 to 456.
v = 'abc';         // changing type of v from int to String.

var v = 123;       // v is of type int.
v = 456;           // changing value of v from 123 to 456.
v = 'abc';         // ERROR: can't change type of v from int to String.

final v = 123;       // v is of type int.
v = 456;           // ERROR: can't change value of v from 123 to 456.
v = 'abc';         // ERROR: can't change type of v from int to String.
查看更多
7楼-- · 2020-05-15 09:20

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.

查看更多
登录 后发表回答