Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
This article also participated in the “Digitalstar Project” to win a creative gift package and creative incentive money.
📝 [Flutter] learning to form a record, [programmer essential knowledge]
📔 — Dart anonymous functions and closures in Flutter!
1. Write at the front
After introducing optional arguments and methods passed as parameters in Dart’s methods in the previous article, let’s move on to learning about anonymous functions and closures in Dart.
The Apple Mac is configured with the Flutter development environment
Android Studio installs a third-party simulator for Flutter — netease MuMu
Failed to find Build Tools Revision 29.0.2
Android license status unknown. Run ‘Flutter doctor — Android – Licenses’
How to create a Flutter project and run your first Flutter project
Dart uses var, final, and const
Dart Indicates the num of the data type
Dart String of data type
Dart data type list&Map
Dart method and arrow function
Dart’s method passes optional parameters and methods as parameters
Anonymous functions
Anonymous functions are also known as anonymous methods, which as their name implies are methods without a method name. Ha ha 😁
- The code for
void functionTest(a) {
// Anonymous methods accept with a variable
var func = (){
print("I'm anonymous method.");
};
func();
}
Copy the code
- The results
Why do we have to use variables here? If you don’t receive it, there is no point in writing it directly. It has no way to execute it. As follows:
(){
print("I'm anonymous method.");
};
Copy the code
It is an anonymous method, there is no method name, how can you call ah, can only use a variable to receive the method address, which can be called directly.
- Execute method immediately
(anonymous method)(anonymous method)(anonymous method)(anonymous method)
((){
print("I'm executing methods now."); }) ();Copy the code
- Code run result
- Anonymous methods as parameters
Remember from the previous blog, forEach traversal, the second argument is a method as an argument, so anonymous functions can be arguments as well.
void functionTest(a) {
var list = [1.2.3.4.5];
// list.forEach(print);
forEachTest(list,(var a){
print(a);
});
}
// create a custom forEach
forEachTest(List list.void func(var element)){
for (var e in list) func(e);
}
Copy the code
- The results
3. The closure
Closure closure
- Concept: A function defined inside a function is a closure, which is also an object.
- What closures do: Access local variables of external functions.
FuncTest returns a closure. Closure is explained by the word funcTest. Closure is a closed loop. Block is also a closure in iOS, and the biggest worry is circular references, but that’s exactly what it is. It is deliberately designed to do so, but also because of the underlying principle of the function, the stack is not restored in time, there will always be this memory.
void closureTest(a){
/ / closure closure
// A function defined inside a function is a closure, which is also an object
// We can access local variables of external functions.
var funcA = funcTest();
funcA();
funcA();
funcA();
}
funcTest(){
int a = 0;
return ()=>print(a++);// This is an anonymous function, the other is a closure
Copy the code
This is designed so that our internal functions can continuously access external variables, as shown in the code example above.
Int a = 0; int a = 0; The local variable A is destroyed, right? So if the value was passed, the above calls would have printed the same result, 1, but they didn’t.
From the printed result, the value of A keeps increasing, which means that A is not completed before the release.
What about another closure? What happens?
The next one is the new stack memory, which is not the same. You can start capturing peripheral variables again.
4. Write in the back
Follow me, more content continues to output
- CSDN
- The Denver nuggets
- Jane’s book
🌹 if you like, give it a thumbs up 👍🌹
🌹 feel harvest, can come to a wave of collection + attention, so as not to find you next time I 😁🌹
🌹 welcome everyone to leave a message exchange, criticism and correction, forwarding please indicate the source, thank you for your support! 🌹