简体   繁体   English

Dart / Flutter 中的类型原语是什么?

[英]What are the type primitives in Dart / Flutter?

What are the type primitives in Dart / Flutter? Dart / Flutter 中的类型原语是什么?

I'm expecting a list that has things like String , Class , Function (?) in it.我期待一个包含StringClassFunction (?) 之类的列表。

I can't seem to find this anywhere in the Dart documentation .我似乎在Dart 文档中的任何地方都找不到这个。

Specifically actually right now I'm writing something where I want to pass in a function as a parameter, so I'm trying to figure out what the function/method type is (assuming that is a thing), but more generally I want the reference to all the Dart type primitives.具体来说,实际上现在我正在写一些我想传入 function 作为参数的东西,所以我试图弄清楚函数/方法类型是什么(假设这是一个东西),但更一般地说,我想要参考所有 Dart 类型原语。

Most Dart types are just class/interface types.大多数 Dart 类型只是类/接口类型。 Unlike Java, Dart does not have "primitive" value types that are not interfaces, so in Dart int , double , String , bool and Null normal interfaces which are subtypes of Object? Unlike Java, Dart does not have "primitive" value types that are not interfaces, so in Dart int , double , String , bool and Null normal interfaces which are subtypes of Object? (and of Object except for Null ), and the values are just normal objects. (以及Object除了Null ),并且这些值只是普通对象。

Dart does have some types and type constructors which are not class/interface types, or which have specific rules preventing you from implementing them. Dart 确实有一些类型类型构造函数,它们不是类/接口类型,或者具有阻止您实现它们的特定规则。 In particular:尤其是:

  • void - Equivalent to Object? void - 相当于Object? , but you're not allowed to use the value. ,但您不能使用该值。 You can return any value from a void function, but no-one is supposed to use it.您可以从void function 返回任何值,但不应有人使用它。
  • dynamic - Equivalent to Object? dynamic - 相当于Object? , but without static type checking. ,但没有 static 类型检查。 You can cast any value to dynamic , and then use it as any type, and you get run-time errors if you make a mistake.您可以将任何值强制转换为dynamic ,然后将其用作任何类型,如果您犯了错误,则会出现运行时错误。
  • Never - an empty subtype of all types. Never - 所有类型的空子类型。 A function returning Never is guaranteed to throw.返回Never的 function 保证会抛出。
  • type Function(argTypes) - A function type. type Function(argTypes) - function 类型。 Some values are functions.有些值是函数。 They're still objects, but are not class/interface instances.它们仍然是对象,但不是类/接口实例。 Subtypes of the interfaces Function and Object .接口FunctionObject的子类型。
  • FutureOr<type> - a supertype of both type and Future<type> . FutureOr<type> - typeFuture<type>的超类型。
  • type? - a nullable type. - 可以为空的类型。 A supertype of both type and Null . typeNull的超类型。

Then the following interfaces have restrictions which prevents you from implementing them in your own classes: Null , int , double , num , bool , String , and Function .然后以下接口有限制,阻止您在自己的类中实现它们: NullintdoublenumboolStringFunction

So, for function types, you write them as, fx, int Function(int, {int y}) .因此,对于 function 类型,您可以将它们写为 fx, int Function(int, {int y})

I think this is what you're looking for: dart:core library :我想这就是你要找的: dart:core library

Built-in types, collections, and other core functionality for every Dart program.每个 Dart 程序的内置类型、collections 和其他核心功能。

Specifically this part: Classes , where you could find:特别是这部分: Classes ,您可以在其中找到:

String A sequence of UTF-16 code units.字符串UTF-16 代码单元的序列。 [...] [...]

Object The base class for all Dart objects. Object所有 Dart 对象的基础 class。 [...] [...]

Function The base class for all function types. Function基础 class 适用于所有 function 类型。 [...] [...]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM