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 method and arrow function!

1. Write at the front

You learned about arrays (lists) and dictionaries (maps) in Dart’s basic data types in the previous article, so let’s move on to learning how methods and functions are represented in Dart’s basic syntax.

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

2. Fill in the blanks

There are two special operators in DART.

  • The assignment operator?? =
  • Conditional operator??
void operatorTest(a){
// Dart operator
/* * assignment operator?? = * conditional operator?? * * /
  / / forvar a; a ?? =10;// Assign 10 when a has no valueprint(a); a ?? =5; print(a); a ?? =1;
  print(a);

}
Copy the code
  • Code run result

When a is initialized, it has no value. = I’m not going to be able to do that, because a already has a value.

Conclusion: If a is nil, it is assigned. If a has a value, it is returned without assigning a new value.

  • Conditional operator??
void operatorTest(a){
// Dart operator
/* * conditional operator?? * /var a; a ?? =10;// Assign 10 when a has no value
  var b ;
  b = 5;
  print(b ?? a);/ /?? Return left if there is a value on the left, return right otherwise

Copy the code
  • The results

  • Return the right-hand value

3. Methods and arrow functions

Everything is an object. In DART, a method is also an object, and the return value and parameter type can be omitted.

The arrow function => can be used when the method’s execution statement is one sentence long

3.1 Method Examples

void main(a){

  functionTest();

}
void functionTest(a){
  print("I am the function");
}
Copy the code
  • The results

3.2 Examples of arrow functions

  • I could change the top one to something like this, same thing

  • For example,
void main(a){

  functionTest();

}
void functionTest(a) {
  print(sum(10.10));
}
// Sum method
int sum (int a,int b){
  return a +b ;
}
Copy the code

As stated above: return values and parameter types can be omitted. Then the deformation is as follows:

// Sum method
sum (a,b){
  return a +b ;
}
Copy the code

But it’s not recommended, it’s not necessary. If the return value and parameter type are omitted, it can be used as an arrow function.

// Sum method
sum (a,b) =>  a +b ;
Copy the code
  • The results

3.3 Arrow function supports triadic operation

void main(a){

  functionTest();

}
void functionTest(a) {
  print(sum(10.10));
}
// Sum method
sum (a,b) =>  a == 10 ? a + b : a - b;

Copy the code
  • The results

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