98

根据这篇文章

您可能知道,dynamic(现在称为)是未提供静态类型注释时的替代类型。

那么,dynamic和 和有什么不一样var?什么时候使用?

4

12 回答 12

112

dynamic是所有 Dart 对象的基础类型。在大多数情况下,您不需要显式使用它。

var是一个关键字,意思是“我不关心这里的类型是什么”。Dart 会将var关键字替换为初始值设定项类型,dynamic如果没有初始值设定项,则默认保留。

var如果您希望变量赋值在其生命周期内发生变化,请使用:

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

final如果您希望变量赋值在其生命周期内保持不变,请使用:

final msg = "Hello world.";

使用final(自由地)将帮助您捕获在您无意更改变量分配的情况下。

final请注意,const当涉及到对象时, 它们之间有很好的区别。final不一定使对象本身不可变,而const

// 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"];
于 2012-09-14T01:07:49.347 回答
69

在DartPad中试试这个:

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

您可以更改 x 的类型,但不能更改 a。

于 2018-10-26T11:30:08.990 回答
61

动态: 可以改变变量的类型,&可以在后面的代码中改变变量的值。

var: 不能改变变量的类型,但可以在后面的代码中改变变量的值。

final: 不能改变变量的类型,&不能在后面的代码中改变变量的值。

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.
于 2020-02-02T14:44:02.220 回答
16

var, 像final, 用于声明一个变量。它根本不是一种类型。

Dart 足够聪明,可以在大多数情况下知道确切的类型。例如,以下两个语句是等价的:

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

另一方面,dynamic它是一种特殊类型,表明它可以是任何类型(又名类)。例如,通过将对象强制转换为dynamic,您可以调用任何方法(假设有一个)。

(foo as dynamic).whatever(); //valid. compiler won't check if whatever() exists
(foo as var).whatever(); //illegal. var is not a type
于 2012-09-14T04:13:37.647 回答
13
var a ;
a = 123;
print(a is int);
print(a);
a = 'hal';
print(a is String);

当没有初始值定义时, var 是动态的

var b = 321;
print(b is int);
print(b);
//b = 'hal'; //error
print(b is String);

当用初始值定义时,在这种情况下 var 是 int 。

于 2019-02-04T18:09:58.650 回答
7

为了澄清以前的一些答案,当您将变量声明为 时dynamic,它的类型会根据您分配给它的内容而变化。当您声明 avar时,一旦分配了某些类型,就会设置类型,之后就无法更改。

例如,下面的代码:

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

在 DartPad 中运行时将返回以下结果:

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

但以下代码甚至无法编译:

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)');

长话短说 -dynamic如果您想要一个非类型变量,请使用var,当您想要一个类型变量时使用您分配给它的任何类型。

于 2019-11-29T15:04:59.700 回答
6

查看以前的答案,我希望这可以澄清/总结一切:

关键字var、final 和 const。这些是声明一个变量(以表明它的存在)(旁注:声明与初始化

然后是String,int,List,dynamic等类型(类型表示变量应该持有什么样的值,这是为了类型安全

通常,我们通过显式声明其类型来声明变量:

String a; // a is now a String type
int b; // b is now an int type

但是我们也可以使用 var 关键字。默认情况下,这会将变量的类型设置为初始化时使用的任何类型。(这称为类型推断

var a = "hello"; // a is now a String type
var b = 5; // b is now an int type

现在,当您尝试使用 var 关键字声明变量但不初始化值时会发生什么?它应该如何推断类型?嗯,还有一种叫做动态的类型。这与通常的 String 或 int 不同,因为它允许为变量分配任何类型的值(通常会出现错误)。

String a = "hello"; // a is now a String type
// var a = "hello"; // Alternative way; same as the line above because its type is inferred to be String
a = 5 // error: A value of type 'int' can't be assigned to a variable of type 'String'

dynamic b; // b is now a dynamic type
b = "hello"; // still a dynamic type, but now its value is of type String  (You can use b.runtimeType to check)
b = 5; // dynamic type, but now its value is of type int

因此,为了解决对文章引用的原始混淆,

您可能知道,动态(现在称为)是未提供静态类型注释时的替代类型。

这只是意味着,如果你没有明确声明它的类型(你使用 var 声明一个变量)并且在没有初始化的情况下这样做,它只是将它的类型推断为动态的:

var b; // b is now a dynamic type, the following will not have any errors.
b = "hello";
b = 5; 
b = true;

其他注意事项:

  • 不知道为什么人们开始谈论 final 和 const,但如果你想了解更多,我认为这里接受的答案很好地解释了它。

  • dynamic a;并且var a;实际上是相同的:它们都声明了一个动态类型的变量。

  • 检查变量类型的两种方法是使用 is 运算符和使用.runtimeType不同的工作方式。请参见以下示例:

    dynamic b; // b is now a dynamic type, no value
    print(b is dynamic); // true
    print(b is Null); // true
    print(b is String); // false
    print(b is int); // false
    print(b.runtimeType); // Null
    
    b = "hello"; // dynamic type, String value
    print(b is dynamic); // true
    print(b is Null); // false
    print(b is String); // true
    print(b is int); // false
    print(b.runtimeType); // String
    
    b = 5; // dynamic type, int value
    print(b is dynamic); // true
    print(b is Null); // false
    print(b is String); // false
    print(b is int); // true
    print(b.runtimeType); // int
    
于 2021-04-30T19:11:58.960 回答
3

在比较动态与 var时可以考虑的方面之一是考虑使用 var 声明和初始化时的行为,同时在动态的情况下不可能更改类型。

但是动态 vs var 不是我要问的问题。我会问更多dynamic 与 Object之间的区别。

这是一个使用 Object 而不是 dynamic 的 DO 注释,表示允许任何对象。

一开始很难感觉到它,但是动态的我会与泛型类型参数有关。

于 2019-05-19T12:26:41.580 回答
3

在dynamic和var中,变量可以保存任何数据类型的数据,即int、float、string等

如果一个变量被声明为动态变量并且即使被初始化,它的类型也会随着时间而改变。在https://dartpad.dev/中试试这个代码

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

}

如果将变量声明为 var,则一旦分配类型就不能更改。

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

上面的代码将导致错误,指出“int”类型的值不能分配给“String”类型的变量 - 第 3 行

但是,如果你在没有初始化的情况下声明一个 var,它就会变成一个动态的:

void main() {
  var x ;
  x = 'abc';
  x=12345;
  print(x);
}
于 2020-08-06T05:59:57.050 回答
1

一个dynamic变量可以改变他的类型,而一个var类型不能改变。

例如 :

var myVar = 'hello';
dynamic myDynamicVar = 'hello';
myVar = 123; // not possible
myDynamicVar = 123; // possible
于 2020-04-24T05:02:19.020 回答
0

dynamic 是一种数据类型,表示 dart 中的所有数据类型

var 是一种类似于“final”的变量声明方式,它采用其值的数据类型

于 2020-08-15T12:47:02.467 回答
0

如果使用 var,则无法更改变量的数据类型。但是如果你使用动态,你可以自由地改变它。例如。

dynamic x = 12; // type: integer
x= "Hello world"; // type: string

如果您使用 var 而不是 dynamic 执行相同操作,这将毫无问题,您将收到错误,因为您无法更改数据类型,因为它在初始化时会自动分配给变量。

于 2020-09-28T06:55:15.183 回答