[Flutter] Anonymous functions and closures in Dart

1. Write in front

Introduced in the previous articleDartThe optional parameters in the method, the method is passed as a parameter , then continue to learn nowDartmiddleanonymous function,Closure.
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)

[Flutter] Dart's method and arrow function

[Flutter] Optional parameters and methods in Dart's methods are passed as parameters

2. Anonymous functions

An anonymous function is also an anonymous method, as the name implies, a method without a method name. haha 😁

  • code example
void  functionTest ( )  { 
// anonymous method accepts 
  var with a variable func =  ( ) { 
    print ( "I am an anonymous method" ) ; 
  } ; 
  func ( ) ; 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • operation result

operation result
Why use a variable to accept it here? If you don't accept it, it's meaningless to write it directly, it can't be executed! as follows:

( ) { 
    print ( "I am an anonymous method" ) ; 
  } ;
  • 1
  • 2
  • 3

It is originally an anonymous method, and there is no method name. How can you call it? You can only use variables to accept the method address, so you can call it directly.

  • execute method immediately

The immediate execution method is the method that is executed when it is written. It does not need to be called. It is executed when it is run. The format:(anonymous method)()

( ( ) { 
    print ( "I am executing the method immediately" ) ; 
  } ) ( ) ;
  • 1
  • 2
  • 3
  • code running result

code running result

  • Anonymous method as parameter

Remember from the previous blog post,forEachTraversal, the second parameter is a method as a parameter, then the anonymous function can also be used as a parameter.

void functionTest() {

  var list =  [ 1 , 2 , 3 , 4 , 5 ] ; 
  // list.forEach(print); 
  forEachTest ( list , ( var a ) { 
    print ( a ) ; 
  } ) ; 
} 
// customize a forEach 
forEachTest ( List list , void  func ( var element ) ) { 
  for  ( var e in list )  func ( e ); 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • operation result

code running result

3. Closures

Closureclosure

  • Concept: A function defined in a function is a closure, and a closure is also an object.
  • The role of closures: local variables that can access external functions.

The following code,funcTestThe return value is a closure. Why there is a closure is well explained by the word closure. A closure is a closed loop. in iOSblockIt is also a closure, and the most worrying problem is the problem of circular references, but this is precisely its characteristic. It is deliberately designed like this, but also because of the underlying principle of the function, the stack will not be restored in time, and there will always be this memory.

void  closureTest ( ) { 
  // Closure closure 
  // The function defined in the function is the closure, and the closure is also an object 
  // The function of the closure: you can access the local variables of the external function; 
  var funcA =  funcTest ( ) ; 
  funcA ( ) ; 
  funcA ( ) ; 
  funcA ( ) ;

} 
funcTest ( ) { 
 int a =  0 ; 
 return  ( ) = > print ( a ++ ) ; //This is an anonymous function, the others are closures

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

This design is to allow our internal functions to continuously access external variables, as you can see from the above code example.

According to our common sense,returnIs it completed after that?int a = 0;local variables ofajust destroyed, right? Then if it is passed by value, the results printed by the above multiple calls should be the same, as1, but the result is not so.

operation result
From the printing results, the value of a has been added, that is, a is released before it is executed.

So what about another closure, what is the result?

operation result
If there is another one, it is the new stack memory, and the two memories are different. The external variable will be captured from the beginning.

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! 🌹

Tags: [Flutter] Anonymous functions and closures in Dart

Flutter flutter android ios dart Cross-platform

Related: [Flutter] Anonymous functions and closures in Dart