[Flutter] Dart's method and arrow function

1. Write in front

Introduced in the previous articleDartArrays of primitive data types (list) and a dictionary (Map), then continue learning nowDartHow to represent methods and functions in the basic grammar of .
Flutter

[Flutter] Apple Mac computer configuration flutter development environment

[Flutter] Android Studio installs a third-party emulator - Netease MuMu

[Flutter] Project running error Failed to find Build Tools revision 29.0.2

【Flutter】flutter doctor 报错Android license status unknown. Run `flutter doctor --android-licenses‘

[Flutter] How to create a new project and run your first flutter project

[Flutter] Basic use of var, final and const in Dart

[Flutter] num of Dart data type

[Flutter] String of Dart data type

[Flutter] Dart's data type list&Map (array and dictionary)

2. Add some content

existdartThere are two special operators.

  • assignment operator? ? =
  • conditional operator? ?
void  operatorTest ( ) { 
// operator in dart 
/*
* assignment operator? ? =
* conditional operator? ?
*
*/ 
  //Example

  var a ; 
  a ? ? =  10 ; //assign 10 when a has no value 
  print ( a ) ;

  a ??= 5;
  print(a);
  a ??= 1;
  print(a);

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • code running result
    code running result

aWhen initialized, there is no value. After it has been assigned, it will be used later.??=Assignment cannot be assigned, because at this timeaIt's already worth it.

in conclusion: ifafornilassign, ifaIf there is a value, it will return the value directly, and will not re-assign a new value.

  • Conditional operator? ?
void  operatorTest ( ) { 
// operator in dart 
/*
* conditional operator? ?
*/ 
  var a ; 
  a ? ? =  10 ; //assign 10 when a has no value 
  var b ; 
  b =  5 ; 
  print ( b ? ? a ) ; // ? ? If there is a value on the left, return to the left, otherwise return to the right

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • operation result

operation result

  • return right value

operation result

3. Methods and Arrow Functions

everything is an object,dartIn the method, the method is also an object, and the return value and parameter type can be omitted.

When the execution statement of the method is only one sentence, you can use the arrow function => expression

3.1 Method example

void  main ( ) {

  functionTest();

} 
void  functionTest ( ) { 
  print ( "I am function" ) ; 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • operation result

operation result

3.2 Examples of Arrow Functions

  • The above can be changed to this, it is the same
    With only one sentence, it can be changed to an arrow function
  • Example
void  main ( ) {

  functionTest();

} 
void  functionTest ( )  { 
  print ( sum ( 10 ,  10 ) ) ; 
} 
// sum method 
int  sum  ( int a , int b ) { 
  return a + b ; 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

It has been said above: the return value and parameter types can be omitted. Then the deformation is as follows:

// sum method 
sum  ( a , b ) { 
  return a + b ; 
}
  • 1
  • 2
  • 3
  • 4

But it is not recommended to write like this, it is not necessary to be inconvenient to read. The return value and parameter type are omitted and there is only one sentence, and it can be turned into an arrow function again.returnYou can just drop it as follows:

// Summation method 
sum  ( a , b )  = >   a + b ;
  • 1
  • 2
  • operation result

operation result

3.3 Arrow functions support ternary operations

void  main ( ) {

  functionTest();

} 
void  functionTest ( )  { 
  print ( sum ( 10 ,  10 ) ) ; 
} 
// sum method 
sum  ( a , b )  = >   a ==  10  ? a + b : a - b ;

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • operation result

code running result

4. Write at the back

Follow me, more content will continue to be output

🌹 If you like it, give it a like👍🌹

🌹 If you think you have something to gain, you can come to a wave of favorites + follow, so as not to find me next time😁🌹

🌹Welcome everyone to leave a message to exchange, criticize and correct,ForwardPlease indicate the source, thank you for your support! 🌹

Related: [Flutter] Dart's method and arrow function