Grammar basics
-
Dart's main function name is void main () {} , you can also add parameters, main (List args)
-
Variables, functions, getters, and setters in Dart can exist independently of the class, with the main method at the top.
-
The modifiers in Dart are public by default, and there are no modifiers like public, private, and protected.
Adding an underscore _ in front of the identifier can make it private. As follows, you can add a getter method to the variable to make it readable. For public member variables, there are implicit setters and getters by default. The getter method definition: directly in the identifier Add get before the character, and use the defined identifier directly when calling;
int _speed = 0;
//通过修改其名称并添加一个getter,使private变量可读
int get speed => _speed;
调用时: A.speed
- Dart's constructor can have no function body, similar to the following:
Bicycle(this.cadence, this.speed, this.gear);
其中this.cadence是简写,可以直接把参数赋值给成员变量,可选参数用{ }括起来,可以赋初始值,如下所示:
Bicycle(int cadence, int speed, int gear) {
this.cadence = cadence;
this.speed = speed;
this.gear = gear;
}
Rectangle({this.origin = const Point(0, 0), this.width = 0, this.height = 0});
- Object creation
After Dart 2, you can omit the new keyword. When you define a variable, you can not write the type because of type inference
var bike = new Bicycle(2, 0, 1);
可简化为:
var bike = Bicycle(2, 0, 1);
- If you know that the value of the variable will not change, you can use final instead of var
- In Dart, single or double quotes can be used to declare the string String
- You can refer to the value in the form of $ {expression} or $ variable name like Kotlin
- Single function expression to use => arrow
- Dart has built-in some commonly used exception classes, you can also customize a class to inherit from the Exception class, or you can directly throw a string of exception information, as follows:
Shape shapeFactory(String type) {
if (type == 'circle') return Circle(2);
if (type == 'square') return Square(2);
throw 'Can\'t create $type.';
}
- Factory pattern in Dart
The factory pattern has some advantages over direct object instantiation: you can hide the details of instantiation; provide a subclass object that returns a corresponding base class; and you can choose to use an existing object instead of creating a new object; ...
Dart uses the keyword factory to create a factory constructor to simplify the definition of factory classes:
abstract class Shape {
factory Shape(String type) {
if (type == 'circle') return Circle(2);
if (type == 'square') return Square(2);
throw 'Can\'t create $type.';
}
num get area;
}
- There is no interface keyword in Dart , because all classes define an interface (every class defines an interface), inheriting a class uses the implements keyword
- Dart supports functional programming. Common operations such as collections are as follows:
String scream(int length) => "A${'a' * length}h!";
main() {
final values = [1, 2, 3, 5, 10, 50];
//非函数式写法
for (var length in values) {
print(scream(length));
}
}
//函数式写法
values.skip(1).take(3).map(scream).forEach(print);