Summary of Dart expressions and syntactic sugar

foreword

There are a lot of syntactic sugar or lambda expressions in the Dart language. The syntax and code size are much simpler, but it adds a lot of trouble to me who wants to get started. When I often look at the official API or third-party documentation API, in the example A large number of similar grammatical sugars are used, which greatly reduces the readability of the code. It takes a lot of searching for articles to understand what this code means. This article is a summary of some grammatical sugars made by myself.

Syntactic sugar or lambda expressions

1. Methods and their fat arrows (=>)

Because dart truly realizes that everything can be an object , the method (Function) in dart is also a class (class) .

Although dart is a strongly typed language, variables can be declared with var. With the dynamic type of dynamic, the type of the variable can be inferred at runtime. The method is no exception. You can omit the return type when declaring the method (Officially not recommended)

as follows:

add(int x,int y){
  return x+y;
}
main(){
  print(add(2, 3));
}

After running, the console is as follows:

5

At this time, if we look at the return value type of the add() method, we will find that it is dynamic—dynamic data type

In daily use, it is recommended not to omit the return type before the method . Clearly pointing out the return type will greatly improve the readability of the code .

In dart, if the method has only one line of statement, you can use the fat arrow (=>) instead of the method body, such as the add() method above

as follows:

int add(int x,int y) => x + y ;

void main(){
  print(add(2, 3));
}

2. call() method 

If a class in dart has a method named "call" , it can be called directly with ()

as follows:

class Computer{
  Computer(){
    print('01');
  }
  Computer.game(){
    print('02');
  }
  void call({int a = 2}){
    print('call $a');
  }
}

void main(){
  Computer();
  Computer()();
  Computer()(a:6);
  final c = Computer();
  c();
  c(a:10);
  Computer.game()();
  Computer.game()(a:111);
}

 The console prints as:

01
01
call 2
01
call 6
01
call 2
call 10
02
call 2
02
call 111

3. Declare nullability (?) and null assertion operator (!)

After dart2.12, null safety is enabled by default, which means that the variables you usually declare cannot be empty when determining the type. For example, the declaration below is wrong , and the compiler will report an error when compiling.

int a = null;

 If you want to declare an empty variable, you can add ? after the type of declaration to indicate that the type can be empty, and the default value of all variables is null when no value is assigned

int? a;

If the variable is declared to be empty, a compile-time error will be reported when the method call or variable may be empty:

 At this time, you can use the null assertion operator to indicate that the object you call is sure not to be empty:

int? getNum() => 1;
void main(){
  var c = getNum();
  print(c!.abs());
}

Or add (?) before (.) to indicate nullable

int? getNum() => 1;
void main(){
  var c = getNum();
  print(c?.abs());
}

4. ?? and ??= Escape operators 

 When declaring a nullable variable, you can use the avoidance operator to assign a value to prevent the variable from being empty

int? a; // = null
a ??= 3;
print(a); // <-- Prints 3.

a ??= 5;
print(a); // <-- Still prints 3.

When using, you can use?? to prevent the variable from being empty

print(1 ?? 3); // <-- Prints 1.
print(null ?? 12); // <-- Prints 12.

5. Cascade operator (..)

In dart, cascading operators can be used to make consecutive calls to the same object:

class Player{
  int ammo = 3;
  void walk() => print('walk');
  void run() => print('run');
  void fire(int a){
    if(ammo >= a){
      print('ta'*a);
      ammo -= a;
    }
  }
}
Player? getPlayer() => Player();
void main(){
  getPlayer()
      ?..walk()
      ..run()
      ..fire(3);
}

This can be used together with null judgment, using (?..) to ensure execution when the object is not null.

Run as follows:

walk
run
tatata

6、lambda

forEach()

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.map(scream).forEach(print);
  //跳过1
  //拿3个结果
  values.skip(1).take(3).map(scream).forEach(print);
}

Guess you like

Origin blog.csdn.net/TDSSS/article/details/129138224