1. Common data types in Dart

In general, there are eight common data types for Dart:

1.1. Numeric types

Num, int, and double are all classes in Dart, meaning they are object level, so their default value is null. This is different from Java’s basic data types.

main(){ num age = 18; //num data type int height =180; //int double weight=62.5; // Double float type print(height/weight is double); //true print(height*age is double); //false print(age/height is double); //true }Copy the code

##### 1.2. Boolean typesCopy the code

Boolean type, as a marker of judgment, is active in logical judgment in all major languages. It has only true and false options. Note that the keyword is bool, not Boolean as in Java.

bool isMan = true;
bool isMarried = false;
Copy the code

1.3. String type

Strings are an integral part of a language, and Dart is no exception. It supports single, double, and triple quotes. Note that the single quotation marks in the single quotation marks need to be escaped, and the characters in the triple quotation marks are output as they are.

String name = 'zhang Feng Jie jie '; David =" There is grace beyond the sea I have never seen before "; // Support String poem="" // support triple quotation marks > land of Zero ---- Zhangfeng Heroic floating floating floating floating, indistinct xi hao-yue feng-qing. Xi began again, numerous xi all things to zero. 2017.11.7 change < br / > "" "; print('${name}\n$proverbs\n$poem'); // Supports the use of variables in stringsCopy the code


1.4. List types

List, as a homogeneous multi-element container, is also a requirement for every language. The List in Dart exists as a class and can be treated like an operable array with a starting index of 0. Dart has an API for almost all array operations in other languages.

List<String> languages = ['Java', 'Dart', 'Python', 'C++', 'Kotlin']; print(languages[0]); //Java languages.add("JavaScript"); // Add elementsCopy the code

1.5. Collection types

Set is an unordered multi-element container that contains no duplicate elements. If you add an existing element, you cannot add it successfully. So it has no index. But there are many ways to manipulate collections

Set<String> languages = {'Java', 'Dart', 'Python', 'C++', 'Kotlin',"Java"}; print(languages); //{Java, Dart, Python, C++, Kotlin} print(languages.add('Java')); //false print(languages.add('JavaScript')); //trueCopy the code

1.6. The Map types

A Map is a container for several key-value pairs. You want to use the name of the mapping. The keys in one Map object cannot be repeated, and the values can be repeated.

Map<int, String> map = {1: 'one', 2: 'two', 3: 'three'}; print(map[3]); //three map[4] = 'four'; print(map.length); / / 4Copy the code

1.7 with Runes

This is a new term in Dart, and it’s a class that inherits from Iterable

, which means it’s a set of ints that are comparable to traversal.

The first line of the String source code reads :A sequence of UTF-16 code units. The Dart string encoding is UTF-16. To view the UTF-16 of a string, use xxx.codeUnits to obtain an int array.

String dart ="Dart"; print(dart.codeUnits); / / [68, 97, 114, 116]Copy the code

For a demon emoji 👿, the corresponding Unicode is \u{1f47f} and take a look at its UTF-16 code

var evil = '\u{1f47f}'; print(evil); / / 👿 print (evil. CodeUnits); / / [55357, 56447]Copy the code

With the xxx.runes method, you can get a runes object and see that an emoji corresponds to a runes element, unlike UTF-16, which requires two codes to be combined. This has one advantage: we can treat emoji as an iterable element.

var evil = '\u{1f47f}\u{1f47a}\u{1f47b}'; print(evil); / / 👿 👺 👻 print (evil. CodeUnits); //[55357, 56447, 55357, 56442, 55357, 56443] print(evil.runes); / / (128127, 128122, 128123)Copy the code

For example, do a map operation on elements in Runes

Runes input = Runes('\u2695\u{1f47a}\u{1f34b}\u2653\u{1f46d}\u{1f34e}\u2694\u{1f470}\u{1f349}'); print(input); //(9877, 128127, 127823, 9861, 128111, 127823, 9877, 128127, 127823) print(String.fromCharCodes(input)); / / ⚕ 👺 🍋 ♓ 👭 🍎 ⚔ 👰 🍉 print (String) fromCharCodes (input. The map ((e) {return e - 5; }))); / / ⚐ 👵 🍆 ♎ 👨 🍉 ⚏ 👫 🍄Copy the code

1.8. Symbols

First of all, there is no doubt that Symbols is also a class. From the name, it is a symbol, and it is also special to use.

In general it identifies a string and retrieves it through mirrorSystem.getName. The advantage is that this flag string is indelible and can be obtained even if the code is confused. Generally used for reflection class names, but reflection is prohibited in Flutter, so there is no DART :mirrors package. This understanding, see the recognition.

Symbol className = #Person; print(className); //Symbol("Person") MirrorSystem.getName(className);Copy the code

As for the specific API operations, I will not elaborate too much here. Generally speaking, it is consistent with the mainstream language, and will be mentioned when used in the code in the future.


Variables and constants in Dart

Dart is a rising star of the new era, so var is indispensable. The types of objects in the Dart language can be derived automatically. That is, the above code can declare variable types with a var keyword.

2.1: Use of the var keyword
var age = 18; var isMan = true; Var name = 'zhangfeng '; var languages = ['Java', 'Dart', 'Python', 'C++', 'Kotlin']; var languages2 = {'Java', 'Dart', 'Python', 'C++', 'Kotlin',"Java"}; var map = {1: 'one', 2: 'two', 3: 'three'}; var className = #Person;Copy the code
2.2: Attention points of VAR

If you just declare a variable with var, you can change the data type after that variable

var who; who="what"; print(who is String); //true who=10; print(who is int); //trueCopy the code

If the declaration is accompanied by an assignment, the type of the object is fixed and cannot be modified

var who="what"; print(who is String); //true who=10; Print (who is int); //trueCopy the code

The following diagram illustrates why: The unassigned value is, the var declares a variable of type Dynamic, and dynamic is also a keyword

dynamic d = 20;
Copy the code


2.3: Definition of constants

If you do not intend to modify a variable later, you can modify it with final or const. When you try to modify its value, an error will be reported.

Final PI = 3.14159265; PI=4; // ERROR: 'PI', a final variable, can only be set once. Const PI = 3.14159265; Pi=4; // ERROR: Constant variables can't be assigned a value.Copy the code

2.4: Difference between const and final

A final variable can be assigned only once: its value can be obtained at run time

A const variable is a compile-time constant: we know before the code runs that it declares the value of the variable as follows, also at the current time. Final f is correct, but const C is wrong, because final can be initialized at run time, but const cannot.

final f = DateTime.now(); // OK
const c = DateTime.now(); // ERROR Const variables must be initialized with a constant value.
Copy the code

3. The operator

A list of common operators

3.1: Arithmetic operators

arithmetic

print(1 + 2); Print (1-2); Print (1 * 2); //2 times print(1/2); Print (% 3); //1 print(10 ~/ 3); / / 3Copy the code

Since the decrement

---->[第 1: I ++]---- int I =3; var a=i++; If ('a=$a, I =$I ') print('a=$a, I =$I '); //a=3, I =4 ---->[第 2: ++ I]---- int I =3; var a=++i; If ('a=$a, I =$I ') print('a=$a, I =$I '); //a=4, I =4 ---->[第 3: I --]---- int I =3; var a=i--; Print ('a=$a, I =$I '); print('a=$a, I =$I '); //a=3, I =2 ---->[第 4: -- I]---- int I =3; var a=--i; Print ('a=$a, I =$I '); print('a=$a, I =$I '); //a=2,i=2Copy the code

3.2: Relational operators

print(1 > 2); //false > print(1 < 2); //true is less than print(1 == 2); False = print(1! = 2); Print (10 >= 3); Print (10 <= 3); //false Is less than or equal toCopy the code

3.3: Bit operators

To illustrate the bit operation, take two numbers: A =1705 and b=17589

To: who is 1 to 1 & both or: | as long as one is one of: to take all the bits are different for 1 left displacement: < < right displacement: > > example: c = a & b 0000 0000 0000 0000 0000 0110 1010 1001 [a] 0x000006a9 1705 & 0000 0000 0000 0000 0100 0100 1011 0101 [b] 0x000044b5 17589 --------------------------- 0000 0000 0000 0000 0100 1010 0001 [C] 0x000004A1 1185 Example: d = a | b 0000 0000 0000 0000 0000 0110 1010 1001 [a] 0x000006a9 1705 | 0000 0000 0000 0000 0100 0100 1011 0101 [b] 0x000044b5 17589 --------------------------- 0000 0000 0000 0100 0110 1011 1101 [D] 0x000046BD 18109 Example: e = ~a 0000 0000 0000 0000 0000 0110 1010 1001 [a] 0x000006a9 1705 ~ 1111 1111 1111 1111 1111 1001 0101 0110 [e] 0xffffF956-1706 Example: f = a ^ b 0000 0000 0000 0000 0000 0110 1010 1001 [a] 0x000006a9 1705 ^ 0000 0000 0000 0000 0100 0100 1011 0101 [b] 0x000044b5 17589 --------------------------- 0000 0000 0000 0100 0010 0001 1100 [f] 0x0000421C 16924 Example: G = a << 4 0000 0000 0000 0000 0000 0000 0110 1010 1001 [a] 0x000006a9 1705 0000 0000 0000 0000 0000 0000 0110 1010 1001 <-- shift 0000 0000 0000 0000 0110 1010 1001 0000 [g] 0x00006a90 27280=1705*2^4 example: H = a >> 4 0000 0000 0000 0000 0000 0000 0000 0110 1010 1001 [a] 0x000006a9 1705 0000 0000 0000 0000 0000 0110 1010 1001 <-- 0000 0000 0000 0000 0000 0000 0110 1010 [g] 0x0000006a 27280=106Copy the code

3.4: Logical operators

The code below says: Women older than 22 or taller than 160 can enter.

bool enter(int age, int height, bool isMan) { return (age > 18 || height > 160) && ! isMan; }Copy the code
3.5: Assignment operator

The next line in the figure above is a combination of the operator and the equal sign, where

A plus b is the same thing as a plus bCopy the code

Other operators and so on, here is the special?? If the value of a variable is null, the assignment statement is executed; otherwise, no assignment is made

---->[case 1: b = null]---- var a = 20; var b; b ?? = a; print(b); //20 ---->[case 2: b is not null]---- var a = 20; var b = 2; b ?? = a; print(b); / / 2Copy the code

3.6: Conditional expressions

Ternary operators: execute the former if the condition is true, otherwise execute the latter

var height =130; var pay = (height>120) ? 200-100; print(pay); / / 200Copy the code

?? Operator: If the former expression value is null, the latter is taken. Otherwise, the latter expression will not be executed

---->[case 1: b = null]---- var a = 20; var b; var c=b ?? a++; print('a=$a,c=$c'); //a=21,c=20 ---->[case 1: b = null]---- var a= 20; var b = 2; var c = b ?? a++; print('a=$a,c=$c'); //a=20,c=2Copy the code

4.Dart functions

The basic composition of a function in Dart is as follows:

4.1: Basic use

Add two numbers

double add(double a,double b){ return a+b; } call add(10,20); / / 30Copy the code

4.2: Optional + Default value

Add the two numbers, and you can discount them

Double add(double a,double b,[double discount=1.0]){return (a+b)*discount; } add(10,20,0.7); / / 21Copy the code

4.3: Attribute parameter

Parameter and a key corresponding, very convenient and clear parameter transfer mode

Double add(double a,double b,{double discount=1.0,double c=0,double d =0}){return (a+b+c+d)*discount; } add(10, 20,discount: 0.7,c: 100); / / 91.0Copy the code

4.4: Function parameters

It is worth noting that the function itself is also an object that can be passed in as a parameter.

double add(double a,double b,deal){ return deal(a)+deal(b); Var fun = (double I) {return I * I; }; print(add(3, 4, fun)); // Find the sum of squares of two numbersCopy the code

4.5: Function shorthand
var fun = (double i) { return i * i; }; Var fun = (I) {return I * I; }; Var fun = (I)=> I * I;Copy the code

In this case, the beginning of the original code should be understandable

void main() => runApp(MyApp()); Void main() {return runApp(MyApp()); }Copy the code

5.Dart process control

It’s a cliche. Write a way to indicate it.

5.1: if… else

If (entry condition){body}else{body}

Double sale(height) {if(height<=0){// return 0; } var price = 100; Var disCount = disCount; // disCount=0; // disCount=0; // disCount=0; }else if(height<160){disCount=0; } return price * disCount; }Copy the code

5.2: For loop

For (initial variable; Exit conditions; Each loop completes the operation of the variable.

for(var count=0; count<10; count++){ print("count:$count"); }Copy the code

5.3: The while loop

While (entry condition){body}

var count=0;
while(count<10){
  print("count:$count");
  count++;
}
Copy the code

5.4: the do… The while loop

Do {body}while(entry condition)

var count=0;
do{
  print("count:$count");
  count++;
}while(count<10);
Copy the code

5.5: Break and continue

For the control of the circulation body. Break exits the loop directly; Continue enters the next loop

---->[break scenario]---- for(var count=0; count<10; count++){ if(count%3==2){ break; } print("count:$count"); ---->[continue scenario]---- for(var count=0; count<10; count++){ if(count%3==2){ continue; } print("count:$count"); 0,1,3,4,6,7,9}Copy the code

5.6: the switch and the case

For very large numbers of branches, switch and case can be used instead of if… The else is simple

var mark='A'; var evaluation; Switch (mark){case'A': evaluation=" excellent "; break; Case 'B': evaluation= 'good'; break; Case 'C': evaluation="; break; Case 'D': evaluation= 'poor'; break; Case 'E': evaluation="; } print(evaluation); / / goodCopy the code

5.7: assert

Assert interrupts program execution if the condition is not met, otherwise the process continues.

assert(1>2); // Program interruptCopy the code

In addition, I have a Flutter wechat communication group. You are welcome to join and discuss Flutter issues together. My wechat account is ZDL1994328. If you want to get a quick taste of Flutter, Flutter 7 is a must-have.

That’s the basic syntax for Dart, so let’s move on to Dart object-oriented