简体   繁体   中英

What are the type primitives in Dart / Flutter?

What are the type primitives in Dart / Flutter?

I'm expecting a list that has things like String , Class , Function (?) in it.

I can't seem to find this anywhere in the Dart documentation .

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.

Most Dart types are just class/interface types. 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.

Dart does have some types and type constructors which are not class/interface types, or which have specific rules preventing you from implementing them. In particular:

  • void - Equivalent to 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.
  • dynamic - Equivalent to Object? , but without static type checking. 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.
  • Never - an empty subtype of all types. A function returning Never is guaranteed to throw.
  • type Function(argTypes) - A function type. Some values are functions. They're still objects, but are not class/interface instances. Subtypes of the interfaces Function and Object .
  • FutureOr<type> - a supertype of both type and Future<type> .
  • type? - a nullable type. A supertype of both type and Null .

Then the following interfaces have restrictions which prevents you from implementing them in your own classes: Null , int , double , num , bool , String , and Function .

So, for function types, you write them as, fx, int Function(int, {int y}) .

I think this is what you're looking for: dart:core library :

Built-in types, collections, and other core functionality for every Dart program.

Specifically this part: Classes , where you could find:

String A sequence of UTF-16 code units. [...]

Object The base class for all Dart objects. [...]

Function The base class for all function types. [...]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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