preface:
The environment has been configured. Next, learn the language developed by fluent, dart.
1, View Dart version
View dart location
2, Create Dart project
First create an empty
Select the project storage location.
Set the location of the sdk.
Set the Module name.
Open demo1.dart to see a file similar to the main file in the OC project.
‘control + r ’ The code can be run. At this time, it runs on the terminal in the AS software and is not triggered on the simulator because it belongs to an empty project.
3, Create a fluent project
Note the project name here. Hump name cannot be used. In addition, the programming language can also be selected. The default setting, Finish, is used here.
Enter the development interface and find main.dart under lib.
[Extension]
Note that the vertical line on the right is the line that controls the number of code lines and divides the display area on the right. If you are not used to it, you can remove the check box from the position in the figure below.
In addition, you can select the maximum number of characters per line in different languages. If it exceeds, it will wrap automatically.
[Extension completed] ↑
4, Grammar
Go back to the main.dart file.
Select the simulator option at the top. We select Chrome (Web). When running, we will automatically call up Google browser and directly minimize the browser to see the AS page.
[tips] web running warning
When the web is used as the running medium, the following warning may be reported.
We only need to input in the project directory of the terminal ' Fluent create. '
1.var,final,const
void varDemo() { //var declares a variable, which can be assigned different types. If it is not initialized, it is null var a; print(a); a = "hello world"; print(a); a = 100; print(a); //final declares a variable that can only be assigned once and cannot be changed. It cannot be used when it is not initialized! final b; //print(b);// An error will be reported here. B is not initialized b = "string"; print(b); //const constant, which must be initialized during definition and cannot be modified. //const c;// An error will be reported here. Const modifies a constant, which must be initialized when defining const c = 22; print(c); }
Console output results:
null hello world 100 string 22
2. Data type
2.1 numerical Number
Contains int, double.
void numDemo() { int a; a = 10; print(a); //a = 1.5;// Here a is of type int. if you assign a value of type double, an error will be reported print("-----"); num b = 11; print(b); b = 2.2; print(b);//num type contains int and double, and no error will be reported print("-----"); double c = 1.5; print(c); c = 12; print(c);//No error reporting print("-----"); print(a.isOdd);//Determine whether a is an odd number print(a.isEven);//Determine whether a is even print("-----"); //Arithmetic operator + - * /% (modulus) ~ / (rounding after dividing) c = 31; print(c~/a); print("-----"); //Data type conversion c = 3.1; print(c.toInt());//double to int print(c.toInt().isOdd); print(a.toDouble()); }
Console output results:
10 ----- 11 2.2 ----- 1.5 12 ----- false true ----- 3 ----- 3 true 10
2.2 String
[Tips] shortcut key for folding code segment: command plus ‘+’ or '-'
void stringDemo() { //The string in Dart is divided into single quotation marks and double quotation marks. The effect is the same var aa = 'hello'; var bb = "world"; print(aa + bb); //You can also create variables of type String String str = 'Mcl'; String str1 = 'Mcl' 'hello' 'world';//Automatic splicing String str2 = 'Mcl\n' 'hello\n' 'world';//It will splice automatically, but the line breaks String str3 = '''Tom Jerry Mark Lee Lucy''';//3. For quotation string, the format between quotation marks will be preserved, such as space, carriage return, etc print(str); print(str1); print(str2); print(str3); //Gets a character in a string print(str[0]); print(str[1]); //Splicing print(str + '!!!'); //Repeatedly output the same string multiple times print(str * 3);//It's too convenient. Wow, ha ha //Output with parameters int a = 1; int b = 2; int c = a + b; print('a + b = ${a + b}');//Note here that ${} should be placed in quotation marks! print('a + b = $c');//If no expression has only one parameter, curly braces {} are not required //Escape String str4 = 'Tom\nJerry'; String str5 = r'Tom\nJerry';//Put a lowercase r before the quotation mark print(str4); print(str5); print(str4.length);//Carriage return counts as one character print(str5.length);//\n is 2 characters, so it is 1 longer than str4 }
Console output results:
helloworld Mcl Mclhelloworld Mcl hello world Tom Jerry Mark Lee Lucy M c Mcl!!! MclMclMcl a + b = 3 a + b = 3 Tom Jerry Tom\nJerry 9 10
2.3 array List
void listDemo() { //list arrays in Dart are also divided into two types, variable and immutable var list1 = [1,2,3];//variable var list2 = const [1,2,3];//Element immutable print(list1); print(list2); //Get one of the elements in the list and change it print(list1[2]); list1[2] = 33; print(list1[2]); // print(list2[2]); // list2[2] = 33;// An error is reported here because const is modified and cannot be modified // print(list2[2]); //Adding elements to a mutable array list1.add(5); print(list1); //Insert element at specified location list1.insert(3, 4); print(list1); //Delete an element list1.remove(33);//Here, the parameter allows you to write the object type, but the write shaping will not report an error // list1.removeAt(0);// There are other methods, such as deleting the last bit and deleting an index interval print(list1); //empty list1.clear(); print(list1); //Array sorting var list3 = [1, 3, 99, 201, 5, 2, 9, 4, 7]; print(list3.sublist(2, 7));//Output unsorted list3 starts with subscript 2 (including 2) and ends with subscript 7 (excluding 7) list3.sort(); print(list3); //Sort and output within the specified interval print(list3.sublist(2, 7));//After sorting, list3 starts with subscript 2 (including 2) and ends with subscript 7 (excluding 7) }
Console print results:
[1, 2, 3] [1, 2, 3] 3 33 [1, 2, 33, 5] [1, 2, 33, 4, 5] [1, 2, 4, 5] [] [99, 201, 5, 2, 9] [1, 2, 3, 4, 5, 7, 9, 99, 201] [3, 4, 5, 7, 9]
2.4 Map (similar to OC Dictionary)
void mapDemo() { //map refers to the key value bisection, which is variable and immutable, similar to the dictionary in OC var map1 = {'one': 'Tom', 'two': 'Jerry'}; print(map1); map1['two'] = 'Lucy'; print(map1); //Immutable var map2 = const {'one': 'Tom1', 'two': 'Jerry1'}; print(map2); // map2['two'] = 'Lucy';// An error will be reported here. The const modified map cannot be modified // print(map2); //common method print(map1.length); print(map1.values); print(map1.keys); //Association between map and list var list1 = ['Tom', 'Jerry', "Mark"]; print(list1.asMap());//Here, the list is transformed into map format, the list element becomes the values of map, and the keys are subscripts 0, 1 and 2 }
Console output results
{one: Tom, two: Jerry} {one: Tom, two: Lucy} {one: Tom1, two: Jerry1} 2 (Tom, Lucy) (one, two) {0: Tom, 1: Jerry, 2: Mark}
3.Dart specific operators
??= and ??
//Operator void operatorDemo() { //??= If the left side is nil, the value is assigned, and if there is a value on the left side, the value on the left side is returned var a; a ??= 6; a ??= 7; print(a);//The output 6 is assigned only once and is not empty //?? If there is a value on the left, the left value is returned; if the left is empty, the right value is returned var b; print(b ?? a);//If b is empty, the value of a is output as 6 b = 8; print(b ?? a);//b is not empty, the value of b is 8 //Both of them judge whether the left side is empty. If it is not empty, it will be taken. If it is empty, it will be taken as the right value. However= It involves assignment, and?? No }
Console output results:
6 6 8
4. Method
four point one Method & arrow function
void main() { functionDemo(); functionDemo1(); } //Arrow function void functionDemo() { /*method * Method is also an object * The return value and parameter type can be omitted * When the method execution statement has only one sentence, you can use the arrow function = > expression and omit the curly braces * */ print('hello'); print(sum(10, 20)); } void functionDemo1() => print('world');//Arrow function //Normal c function int sum (int a, int b) { return a + b; } //After using the arrow function instead, the return value int, parameter type int, and 'return' can be omitted, and even the ternary operator?: sum1 (a, b) => a + b;
Console output results:
hello 30 world
4.2 method & optional parameters
/*sum3()b and c in are optional parameters, which can be omitted when calling sum3, * In addition, the order may not be considered when assigning b and c, but the parameter name must be filled in * b The parameter types var of and c can not be written * */ sum3 (int a, {var b, var c}) { b ??= 0;//nil is assigned to 0 c ??= 0; return a + b + c; } /*sum4()b and c in are also optional parameters, but when assigning b and c, because they are parentheses, * Therefore, the order should be considered, but it is not necessary to fill in the parameter name * B & C will report an error if the int type is set without initializing the assignment * */ sum4 (int a, [int b = 0, int c = 0]) { return a + b + c; } //If b in sum4 is not initialized, you can add a question mark after int?, If the value is assigned in the method, no error will be reported //int? It is called nullability, which tells the compiler that the latter is empty //Here sum5 is equivalent to sum4 sum5 (int a, [int? b, int? c]) { b ??= 0; c ??= 0; return a + b + c; } functionDemo2 () { sum3(10, c:5, b:4);//Output 19 sum3(10);//Output 10 sum4(10, 4, 5);//Output 19 sum4(10, 4);//Output 14 optional no assignment c sum5(10, 4, 6);//Output 20 sum5(10, 4);//Output 14 optional no assignment c }
Console output functionDemo2() result:
19 10 19 14 20 14
4.3 methods & methods are passed as parameters
void main() { functionDemo3(); } printHello() { print('hello'); } //The method name can be passed as a parameter assignment void functionDemo3() { var hello = printHello;//Method name assignment hello(); var list = [1, 2, 3, 4]; list.forEach(print); //Look at the source code of forEach // void forEach(void f(E element)) { // for (E element in this) f(element); // } //f == print in forEach. This is the parameter passed. E refers to list //Then list. For each (print) is equivalent to printing each element in the list list.forEach(myPrint); myForEach(list, print); //In fact, it feels like a callback or block in OC. A print or myPrint is called here; //The specific content in myPrint is implemented over there, which is more like a block. } //Copy a list. For each (print)? int b = 0; void myPrint(var a) { b++; print('element $b = $a'); //a is each element myprint in the list, which is equivalent to f in forEach } //How do we write a forEach? void myForEach(List list, void func(var element)) { for(var e in list) func(e);//Execute the func method for each value in the list }
Console output results:
hello 1 2 3 4 Element 1 = 1 Element 2 = 2 Element 3 = 3 Element 4 = 4 1 2 3 4
5. Anonymous function
void main() { functionDemo4(); } //1. Anonymous method: method without method name void functionDemo4() { //In this way, anonymous methods cannot exist alone and must be assigned to other variables before they can be called var func = () { print('I'm anonymous'); }; func(); //2. Execute the function immediately (() { print('Execute function now'); }) ();//The front is wrapped in parentheses, which is equivalent to this () (); //The first bracket is an anonymous function, and the last bracket is 0 parameters contained in the method //3. Anonymous method instance: like our list.forEach(myPrint); var list = [1, 2, 3, 4]; //You can write it int b = 0; list.forEach((var a) { b++; print('element $b = $a'); });//forEach is an anonymous function, which is more like a block. }
Output results:
I'm anonymous Execute function now Element 1 = 1 Element 2 = 2 Element 3 = 3 Element 4 = 4
6. Closure
void main() { closureDemo();//closure } // --------- //Closure: a function defined in a function. Closure is also an object. //Function: local variables of external functions can be accessed. void closureDemo() { var func = funcA(); func(); func(); func(); func();/*4 Each call normally prints 0, but the result is 0, 1, 2, 3 *Because func is a closure, it will always hold funcA, and count will not be released */ var func2 = funcA(); func2(); func2(); func2(); func2();/*After re assignment, count starts from 0 again *Because func2 is a new closure */ } funcA () { int count = 0; return () => print(count++);//Returns an anonymous function }
Output results:
0 1 2 3 0 1 2 3
Summary:
So far, the basic syntax of Dart is briefly introduced here ~ just a basic understanding. There are many additional usages, which we will explore later~