据我所知,对于静态类型,数据类型在编译时已知,对于动态类型,数据类型在运行时已知/评估。
Dart文档(https://dart.dev/faq#q-is-dart-a-statically-typed-language)说dart是一种静态类型的语言-
Q. Is Dart a statically typed language?
Yes, Dart 2 is statically typed. For more information, read about Dart’s type system.
With its combination of static and runtime checks, Dart has a sound type system, which guarantees that an expression of one type cannot produce a value of another type. No surprises!
Even with type-safe Dart, you can annotate any variable with dynamic if you need the flexibility of a dynamic language. The dynamic type itself is static but can contain any type at runtime. Of course, that removes many of the benefits of a type-safe language for that variable.那么,我们如何能够分配一个变量“动态”数据类型,这使得类型评估在运行时?
发布于 2022-05-25 08:05:37
简单地说,dynamic 是,它本身就是一种类型。它只是将类型检查移到运行时(如您引用的文本中所提到的)。
这允许您执行像final Map<String, dynamic> json = await fetchData();这样的操作,否则这是不可能的。
请注意,Dart强烈不鼓励使用dynamic类型,特别是因为它的运行时影响和缺乏“静态性”。
发布于 2022-05-25 08:07:34
动态是其中一种类型。
因此,如果要使用动态变量,则必须将其转换为其他类型。
https://stackoverflow.com/questions/72373634
复制相似问题