The function 7.Dart arrow anonymous function closure function

1. Arrow function

Arrow line of code functions only

/ * Required: forEach print data using the following List inside * / 
void main () { 
    List List = [ 'apple', 'banana', 'watermelon']; 


    // list.forEach ((value) { 
    // Print ( value); 
    //}); 

    list.forEach ((value) => Print (value)); 


    // list.forEach ((value) => { 
    // Print (value) 
    //}); 
}
main void () { 
/ * Demand: List which modify the following data, so a value greater than 2 * in the array is multiplied by 2 / 

    List List = [4,1,2,3,4]; 

   var newList = List.map ( (value) => value> 2 value * 2: value); // trinocular expression? 

   Print (newList.toList ()); 
}

 

2. The function of each call

When the function is more content, they can call each other, reducing the amount of code

void main () { 
  / * 
requirements: 1, define a method isEvenNumber to determine whether a number is an even number   
         2, defines a method for printing 1-n within all even 
* / 
    BOOL isEvenNumber (int n-) { 
      IF (n-% 2 = = 0) { 
        return to true; 
      } 
      return to false; 
    } 

    printNum (int n-) { 
        for (var I =. 1; I <= n-; I ++) { 
            IF (isEvenNumber (I)) {// call isEvenNumber method of 
              print (i) ; 
            } 
        } 
    } 

    printNum (10); 
}

3. anonymous methods

void main(){
  var printNum=(){

    print(123);
  };

  printNum();
}

 

  

  

  

  

Guess you like

Origin www.cnblogs.com/The-Chao/p/11910897.html