The assignment operator

  1. = plain assignment
  2. ?? = the value is assigned when the variable value is null
void main() { var a = 10; var b; b ?? = 5; print(b); var c = 20; c ?? = 5; print(c); }Copy the code

The printed result is:

5
20
Copy the code

In the above code, b is not initially assigned to null. = Assignment takes effect, so print b with a value of 5; C = null; Lambda doesn’t work. C is still 20.

Conditional expression

  1. The trinary operator: condition? expr1 : expr2
  2. ?? Operator: expr1?? Expr2 When expr1 is null, expr2 is used
void main() { int gender = 0; var str = gender == 0 ? "Male" : "Female"; print(str); var a; var b = "Dart"; print(a ?? b); // if a is null, use b's value a = "Java"; print(a ?? b); // if a is null, use the value of b}Copy the code

The printed result is:

Male
Dart
Java
Copy the code

Control statements

Dart control statements are almost identical to Java control statements, which I’ll cover briefly.

Conditional statements

  • If statement
  • if… Else statements
  • if… else if… Else statements

For statement

Format: for(assign initial value; Judgment condition; For (var I = 0; i <= 5; i++) { }Copy the code
For (element in list){} var list = [1,2,3]; for(var item in list){ }Copy the code

The while loop

  • The while loop
  • do… The while loop

Break and continue

switch case

methods

Method definition

Return type method name (parameter 1, parameter 2…) {method body… Return Return value}

Such as:

int getArea(int width, int height){
  return width * height;
}
Copy the code

Method properties

  • Methods are also objects and have a concrete type Function
  • The return value type and parameter type can be omitted
  • Arrow syntax: =>expr is {return expr; } abbreviation, applies to only one expression
  • Methods have a return value. If not specified, return null is default

GetArea () can be defined as follows:

  1. Omit the return value type
getArea(int width, int height){
  return width * height;
}
Copy the code
  1. Omit parameter type
int getArea(width, height){
  return width * height;
}
Copy the code
  1. Omit the return value type and parameter type
getArea(width, height){
  return width * height;
}
Copy the code
  1. Arrow grammar
getArea(width, height) => width * height;
Copy the code

Void printvoid printvoid printvoid printvoid printvoid printvoid

Void main() {print(printPerson(", 31)); void main() {print(printPerson(", 31)); } printPerson(name,age){ print("name = $name, age= $age"); }Copy the code

The printed result is:

Name = Age = 31 nullCopy the code

Optional parameters

  • Optional named parameters: {params1, params2… }
  • Optional position parameters: [params1, params2,…]

Optional named parameters can be passed in accordance with the specified parameter name rather than the position of the parameter. Optional location parameters can only be passed in accordance with the position of the parameter.

Demonstration of optional named parameters:

Void main() {printPerson(" yellow "); PrintPerson (" huang Jiaju ", age: 31); PrintPerson (" Huang Jiaju ", gender: "Male"); PrintPerson (" Huang Jiaju ", age: 31, gender: "Male"); } printPerson(String name, {int age, String gender}){ print("name = $name, age= $age, gender=$gender"); }Copy the code

You can see that optional named parameters can be passed or not. To pass optional named parameters, you need to specify the parameter name

Demonstration of optional position parameters:

Void main() {printPerson2(" yellow "); PrintPerson2 (" Wong Ka Kui ", 31); PrintPerson2 (" huang Jiaju ", 31, "Male"); } printPerson2(String name, [int age, String gender]){ print("name = $name, age= $age, gender=$gender"); }Copy the code

You can see that the optional positional parameters can be passed or not, and if they are passed in, they need to be passed in order

The default parameters

  • Use = to specify default values in optional arguments
  • The default value can only be compile-time constants
Void main() {printPerson(" yellow "); PrintPerson (" huang Jiaju ", age: 58); PrintPerson (" Huang Jiaju ", age: 58, gender: "Male"); } printPerson(String name, {int age = 31, String gender = "Male"}){ print("name = $name, age= $age, gender=$gender"); }Copy the code

The printed result is:

Name =Male name =Male, age= 31, gender=Male name =Male, age= 58, gender=Male name =Male, age= 58, gender=MaleCopy the code

The optional arguments in the printPerson() method in the code above specify a default value of 31 for age, and a default value of “Male” for gender, which is used when no age is passed, and a default value of “Male” when no gender is passed.

The method object

  • Methods can be assigned as objects to other variables
void main() {
    var func = printHello;
    func();
    
}

printHello(){
  print("Hello");
}
Copy the code

You can see that the printHello() method can be assigned as an object to the variable func, which simply needs to be called as a method to execute.

  • Methods can be passed as parameters to other methods

ForEach () = print (); forEach() = print (); forEach() = print ();

  void forEach(void f(E element)) {
    for (E element in this) f(element);
  }
Copy the code

ForEach receives a void method, while print can be passed in as an argument, and print can be called one by one through the list to print the current element.

Anonymous methods

  • Can be assigned to a variable and called from a variable
void main() { var hello = (){print("Hello"); }; hello(); }Copy the code

You can see that an anonymous method that prints “Hello” is directly assigned to the Hello variable and then called as a method.

The above can also be written as:

((){print("Hello"); }) ();Copy the code

However, this method is not recommended because it is called without a name and is not easy to understand.

  • Can be called directly in other methods or passed to other methods

Similar to the above demonstration method passed as a parameter:

void main() { var list = ["1","2","3"]; list.forEach((item){print(item * 3); }); }Copy the code

The forEach() method on list is called, passing in an anonymous method that prints each element iterated three times. The result is:

111
222
333
Copy the code

closure

  • A closure is a method (object)
  • Closures are defined inside other methods
  • Closures can access local variables inside external methods and hold their state
void main() {
    var func = a();
    func();
    func();
    func();
}

a(){
  int count = 0;
  printCount(){
    print(count++);
  }
  return printCount;
}
Copy the code

Declare a closure printCount in the a() method, expose it, receive the a() method through the variable func, and make multiple calls, printing the result:

0
1
2
Copy the code

You can see that the count value increases in sequence, indicating that the closure can access and hold the state of a local variable inside an external method.

Closure can also be returned using anonymous methods:

a(){
  int count = 0;
  return (){
    print(count++);
  };
}
Copy the code

This chapter has been introduced, interested partners can continue to see chapter 3:

Dart Chapter 3 — an introduction to object-oriented programming and the like