Dart 区别于;var";及;“动态”;输入省道?

Dart 区别于;var";及;“动态”;输入省道?,dart,Dart,根据: 您可能知道,dynamic(现在称为)是未提供静态类型注释时的替代类型 那么,dynamic和var之间有什么区别呢?何时使用?动态是所有省道对象的基础类型。在大多数情况下,您不需要显式地使用它 var是一个关键字,意思是“我不想记下这里的类型。”Dart将用初始值设定项类型替换var关键字,或者如果没有初始值设定项,则默认情况下将其保留为动态 如果期望变量赋值在其生命周期内发生变化,请使用var: var msg = "Hello world."; msg = "Hello world

根据:

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


那么,
dynamic
var
之间有什么区别呢?何时使用?

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

var是一个关键字,意思是“我不想记下这里的类型。”Dart将用初始值设定项类型替换var关键字,或者如果没有初始值设定项,则默认情况下将其保留为动态

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

var msg = "Hello world.";
msg = "Hello world again.";
final msg = "Hello world.";
如果期望变量赋值在其生命周期内保持不变,请使用final

var msg = "Hello world.";
msg = "Hello world again.";
final msg = "Hello world.";
使用final(自由地)将有助于您捕捉在无意中意外更改变量赋值的情况

请注意,对于对象,finalconst之间有很好的区别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"];

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
是一种特殊类型,表示它可以是任何类型(aka类)。例如,通过将对象强制转换为
dynamic
,您可以调用任何方法(假设存在)

请尝试以下方法:

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

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

>P> >在比较<强>动态VAR 中,考虑到使用VARE声明初始化时的行为,同时在动态IS情况下不可能改变类型。 但动态vs var不是我要问的问题。 我想问更多的是动态和对象之间的区别是什么

这是一个


一开始很难感觉到它,但我会将其与泛型类型参数联系起来

为了澄清前面的一些答案,当您将一个变量声明为
动态
时,它的类型会根据您分配给它的内容而变化。当您声明一个
var
时,该类型在分配了某个内容后即被设置,之后不能更改

例如,以下代码:

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)');
var bar='bar';
打印('bar为${bar.runtimeType}($bar)');

bar=123;// 动态:可以更改变量的类型,&可以在以后的代码中更改变量的值

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.
var:不能更改变量的类型,但可以在以后的代码中更改变量的值

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

dynamic
变量可以更改其类型,
var
类型不能更改

例如:

var myVar = 'hello';
dynamic myDynamicVar = 'hello';
myVar = 123; // not possible
myDynamicVar = 123; // possible

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

如果一个变量被声明为动态变量,甚至被初始化,它的类型会随着时间的推移而改变

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

void main() {
  var x = 'abc';
  x = 12345;
  print(x);
}
上述代码将导致错误,说明无法将“int”类型的值分配给“String”类型的变量-第3行

但是,如果您在未初始化的情况下声明一个var,它将成为一个动态变量:

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

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


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

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


如果使用var而不是dynamic执行相同操作,则不会出现任何问题,因为无法更改数据类型,因为它在初始化时会自动分配给变量。

查看前面的答案,我希望这可以澄清/总结所有问题:

关键字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关键字声明变量,但不初始化值时会发生什么情况?它应该如何推断一种类型?还有一种叫做动态的。这与通常的字符串或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,但我认为公认的答案很好地解释了这一点
    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 b; // b is now a dynamic type, the following will not have any errors.
    b = "hello";
    b = 5; 
    b = true;
    
    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