8, functions,
Dart is an object-oriented language, so functions are objects, and functions are of type Function.
1. Basic use of functions
The parameters of a function can be divided into two categories: required and optional
Optional parameters can be classified into named optional parameters and location optional parameters
Definition method:
Name Optional parameters: {param1, param2... } Position Optional parameters: [param1, param2,...]Copy the code
1. Main function
Every application must have a top-level main function. It is an entry point to an application. The main function returns void and has an optional List argument
void main() {
runApp(MyApp());
}
Copy the code
2. Optional parameters
Optional named parameters, even if they are not passed. When defining functions, use {param1,parqm2… } specify named parameters.
void userSettings({int age,String name}){ if(age ! = null) { print('my age is ${age}'); } if(name ! = null) { print('my name is ${name}'); }}Copy the code
In the above function, we can pass age and name, draw either, or pass neither
3. Mandatory parameters
Sometimes, when we call a function, we have to pass in some parameters, and this is the case with the @required modifier. Using @required facilitates checking by a static code analyzer
void userSettings({@required int age,String name}){ if(age ! = null) { print('my age is ${age}'); } if(name ! = null) { print('my name is ${name}'); }}Copy the code
4. Optional position parameters
Use [] to mark the target as an optional positional parameter
void userSettings(int age, String name, [String interests]){ if(age ! = null) { print('my age is ${age}'); } if(name ! = null) { print('my name is ${name}'); } if(interests ! = null){print(' interest is ${interests}'); }}Copy the code
5. Default parameters
The default value is a compile-time constant, which is assigned to function arguments using =
- Arguments can have default values, which are used if not passed in
- Note that only optional parameters can have default values. Mandatory parameters cannot have default values
Void userSettings(int age, String name, [String interests,String sex = "male "]){if(age! = null) { print('my age is ${age}'); } if(name ! = null) { print('my name is ${name}'); } if(interests ! = null){print(' interest is ${interests}'); } if(sex ! = null){print(' gender ${sex}'); }}Copy the code
6. Functions as arguments
executeFunction(fn, [args]) {
if (fn != null) {
return fn(args);
}
}
Copy the code
7. Functions as variables
var say = (name){ pring(name); } say(' xiaoming ');Copy the code
Arrow function
List l1 = [1, 2];
l1.forEach((item) {
print(item);
});
l1.forEach((item) => print(item));
Copy the code
The arrow function can have only one line and can omit the curly braces.
var l2 = l1.map((item) {
return item % 2 == 0 ? item * 2 : item;
});
print(l2.toList());
var l3 = l1.map((item) => item % 2 == 0 ? item * 2 : item);
print(l3);
Copy the code
3. Anonymous functions
In some cases, naming a function is too cumbersome. Instead, we can use a function without a name, which can be called anonymous function, lambda or closure.
main(List<String> args) { // 1. Var movies = 'Inception ',' Interstellar ', 'Life of PI ',' Journey to the West ']; PrintElement (item) {print(item); } movies.forEach(printElement); ForEach ((item) {print(item); }); movies.forEach((item) => print(item)); }Copy the code
Execute the function immediately
Print ('my name is ${name}'); })('postbird');Copy the code
5, closures
Function getA() {int a = 0; return () { a++; print(a); }; } Function b = getA(); b(); b();Copy the code