<> introduce

Dart yes Flutter The main development language of ,Flutter It's one from Google For mobile application development
SDK. In fact, for Java For developers Dart, It's relatively easy to understand , This note will record the beginning of learning Dart Time , Comparison Java Some of the differences between .

<> difference

<> Constructor

stay Dart There can be no method body in the construction of .
// It's easy to write like this User(this.cadence, this.speed, this.gear); // It can also be written like this Bicycle(int
cadence, int speed, int gear) { this.cadence = cadence; this.speed = speed;
this.gear = gear; }
<> Optional parameters

stay Dart You can use optional parameters in , Whether it's construction or common method .
Book({this.money = 100, this.size = 0, this.author = null});
The parameter name should be specified when calling
main() { print(Book(money:100 , size: 88, author:'test'));
print(Book(money:99, size:100)); print(Rectangle()); }
<> keyword

* stay Dart There is no such thing as public,private,protected Such key words .
If an identifier uses (_) start , The identifier It is private in the library
* Dart2 in , It can not be used new Keyword creation , It can also be used .
<> variable

* Dart The initial value of the variable is null, even if it is int So is the type null.
* Dart Support self identification , It can be used var Variable definition , You can also specify specific types directly .
* If you don't plan to change a variable in the future , use final perhaps const. One final Variable can only be assigned once ; One const Variables are compile time constants .
<> Basic types

* int and double yes num Subclasses of . No, float type .
* Dart All classes in are Object Subclasses of .
* String You can use single or double quotation marks .
* String Support templates , use ${expression} To achieve the effect of string template , If the expression is just an identifier , You can also remove the curly brackets
* String prefix r Represents the original string .
* only true and flase yes bool type , These two objects are also compile time constants . When Dart When a Boolean value is required , only true Objects are considered true, Everything else is false.
<> method

* Dart It's a real object-oriented language , Methods are also objects and have one type Function.
* You can use the options mentioned above .
* It supports setting default parameter values directly .
* All functions return a value , If no return value is specified , be Default statement return null; Execute as the last statement of the function .
* You can call another method as a parameter . printElement(element) { print(element); } var list = [1,
2, 3]; // Pass printElement as a parameter. list.forEach(printElement);
* Method can also be assigned to a variable var loudify = (msg) => '!!! ${msg.toUpperCase()} !!!';
assert(loudify('hello') == '!!! HELLO !!!');
<> Operator

* ?? expr1 ?? expr2 If expr1 yes non-null, Returns its value ; Otherwise, it will be executed expr2 And return the result .
.? The object on the left side of the expression can be avoided null Throw an exception when .

Cascade operator , Cascade operator (…) Can be on the same object Call multiple functions continuously and access member variables .
querySelector('#button') // Get an object. ..text = 'Confirm' // Use its
members. ..classes.add('important') ..onClick.listen((e) =>
* Type decision operator
as, is, and is! The operator determines the object at run time Type of operator :

<>as Type conversion

<>is If the object is of the specified type True

<>is! If the object is of the specified type False
Only when obj Yes T Interface of , obj is T actually is true. for example obj is Object always true.
<> Assertion

Dart Chinese support assert This assertion method . If the method result true, Carry on , If false An exception is thrown . This method will not be executed in the formal package .
// assert( condition );
## summary

Dart and Java It's still very similar , about Java Programmers are also relatively easy to use , Because it's a beginner now , It only summarizes some common ones , Of course, the difference is much more than that , Most of the summaries are Dart Some of them Java Something not in , Will continue to add .