// Define a function.voidprintInteger(intaNumber){print('The number is $aNumber.');// Print to console.}// This is where the app starts executing.voidmain(){varnumber=42;// Declare and initialize a variable.printInteger(number);// Call a function.}
Everything you can place in a variable is an object, and every object is an instance of a class.
all objects inherit from the Object class.
Dart is strongly typed
You can make a variable nullable by putting a question mark (?) at the end of its type. For example, a variable of type int? might be an integer, or it might be null.
If you enable null safety, variables can’t contain null unless you say they can.
null is an object
Dart supports generic types, like List<int>
If an identifier starts with an underscore (_), it’s private to its library.
A const variable is a compile-time constant. (Const variables are implicitly final.)
Instance variables can be final but not const.
You can also use const to create constant values.
Although a final object cannot be modified, its fields can be changed. In comparison, a const object and its fields cannot be changed: they’re immutable.
Runes (Runes; often replaced by the characters API)
Symbols (Symbol)
The value null (Null)
Object: The superclass of all Dart classes except Null.
Enum: The superclass of all enums.
Future and Stream: Used in asynchrony support.
Iterable: Used in for-in loops and in synchronous generator functions.
Never: Indicates that an expression can never successfully finish evaluating. Most often used for functions that always throw an exception.
dynamic: Indicates that you want to disable static checking. Usually you should use Object or Object? instead.
void: Indicates that a value is never used. Often used as a return type.
To get the string corresponding to an object, Dart calls the object’s toString() method.
Dart
123456789
vars1='''You can createmulti-line strings like this one.''';vars2="""This is also amulti-line string.""";vars=r'In a raw string, not even \n gets special treatment.';
The main() function returns void and has an optional List<String> parameter for arguments.
Dart
12345678
// Run the app like this: dart args.dart 1 testvoidmain(List<String>arguments){print(arguments);assert(arguments.length==2);assert(int.parse(arguments[0])==1);assert(arguments[1]=='test');}
assert(5/2==2.5);// Result is a doubleassert(5~/2==2);// Result is an int
Type test operators
as Typecast
is True if the object has the specified type
is! True if the object doesn’t have the specified type
condition ? expr1 : expr2expr1 ?? expr2
Cascade notation
Dart
1 2 3 4 5 6 7 8 910
varpaint=Paint()..color=Colors.black..strokeCap=StrokeCap.round..strokeWidth=5.0;querySelector('#confirm')// Get an object.?..text='Confirm'// Use its members...classes.add('important')..onClick.listen((e)=>window.alert('Confirmed!'))..scrollIntoView();
Dart
1234567
varmessage=StringBuffer('Dart is fun');for(vari=0;i<5;i++){message.write('!');}varcollection=[1,2,3];collection.forEach(print);// 1 2 3
Use a type test operator rather than runtimeType to test an object’s type. In production environments, the test object is Type is more stable than the test object.runtimeType == Type.
构造函数:
Dart
1 2 3 4 5 6 7 8 91011
classPoint{doublex=0;doubley=0;Point(doublex,doubley){// See initializing formal parameters for a better way// to initialize instance variables.this.x=x;this.y=y;}}
Use this only when there is a name conflict. Otherwise, Dart style omits the this.
Dart
1 2 3 4 5 6 7 8 9101112131415161718
classRectangle{doubleleft,top,width,height;Rectangle(this.left,this.top,this.width,this.height);// Define two calculated properties: right and bottom.doublegetright=>left+width;setright(doublevalue)=>left=value-width;doublegetbottom=>top+height;setbottom(doublevalue)=>top=value-height;}voidmain(){varrect=Rectangle(3,4,20,15);assert(rect.left==3);rect.right=12;assert(rect.left==-8);}