[Flutter] Optional parameters and methods in Dart's methods are passed as parameters
1. Write in front
Introduced in the previous articleDart
method and arrow function , then continue to learn nowDart
Optional parameters in the method, the method is passed as a parameter .
[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
2. Optional parameters
First, let's take a look at the optional parameters in the method
sum1 (int a ,{b,c}){
b ??= 1;
c ??= 2;
return a + b + c;
}
- 1
- 2
- 3
- 4
- 5
In the above method,b
andc
It is an optional parameter, and it will also prompt when calling:
because optionalb
andc
The parameter does not specify a type, so it isdynamic
dynamic type.
Optional parameters: When calling the method, you must bring the name of the formal parameter
void functionTest ( ) {
print ( sum1 ( 1 , c : 2 , b : 3 ) ) ;
}
// Optional parameters: When calling a method, you must include the names of the formal parameters, and the order can be different
sum1 ( int a , { b , c } ) {
//If b/c is empty, assign 1, 2
b ? ? = 1 ;
c ? ? = 2 ;
return a + b + c;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Optional parameters, you can choose to give a value, or you can choose not to give it, if there are multiple optional parameters, you can choose one of the values.
Can optional parameters be added with types, as follows?
sum1 (int a ,{int b,int c}){
- 1
This will give an error, as follows:
Error: The value of parameter 'b' cannot be 'null' because its type is 'int', but the implicit default value is 'null'.
// nullability
sum1 ( int a , { int ? b , int ? c } ) {
// int? tells the compiler that I've handled the null case
// if b/c is null assign 1, 2
b ? ? = 1 ;
c ? ? = 2 ;
return a + b + c ;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
add a?
That's it, just tell the compiler that this isnullability
Type, tell the compiler that if it is empty, I will handle it myself later.
Do not use?
If so, just assign the default value directly.
sum1 (int a ,{int b=1,int c=2}){
// b ??= 1;
// c ??= 2;
return a + b + c;
}
- 1
- 2
- 3
- 4
- 5
The above optional parameters need to have a formal parameter name, which can be specified tob
still isc
Assignment, the following way of writing, does not require formal parameter names, but cannot specify assignment
sum1 (int a ,[int b=1,int c=2])
- 1
If you specify an assignment, an error will be reported, as follows:
skipping one of the middle will also report an error, as follows:
can only be assigned in order
All optional parameters are assigned as follows:
print(sum1(1,2,3));
- 1
3. Methods are passed as parameters
A method is also an object, which can be assigned to a variable and can be called directly to run
void functionTest ( ) {
var hello = jpHello ( ) ;
hello ( ) ; // can be called directly
}
jpHello ( ) {
print ( "hello reno" ) ;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
method is passed as a parameter, of course it and weOC
middleblock
Not the same, the usage is similar, so let's take a look at the example below.
void functionTest() {
var list = [1,2,3,4,5];
list.forEach(print);
}
- 1
- 2
- 3
- 4
Here is the call to the systemforEach
method, passing in a method parameterprint
Printlist
content.
let's go seeforEach
Internal implementation of the method
forEach
accept an incomingaction
method, then traverse the caller of the methodthis
every element insideelement
, then callaction
, incomingelement
.
Then we will implement oneforEach
function method!
void functionTest() {
var list = [1,2,3,4,5];
// list.forEach(print);
forEachTest(list,print);
}
//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
The test print result is as follows:
this isdart
The method is used as a parameter, that is, an object, and parameters can be passed, which is also the most used scenario.
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,
Forward
Please indicate the source, thank you for your support! 🌹