Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

📝 [Flutter] learning to form a record, [programmer essential knowledge]

📔 — Dart uses var, final, and const.

1. Write at the front

You have covered the installation and configuration of the Flutter environment and resolved some of the problems encountered. Now start learning the syntax of Flutter!

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

Let’s start with the basics of var, final, and const.

2. var

Var is the definition of a variable.

void main(a) {
  var a;/ / variable
  print(a);
}
Copy the code

If you define a variable but do not assign a value, and do not know the data type of the variable, what is the result of printing it?

I’m going to print null here, so now I’m going to assign a and see what happens if I print it?

void main(a) {
  var a;/ / variable
  a = "this is a !";
  print(a);
}
Copy the code

The output is as follows:

If the string assigned to a is printed successfully, then assign another value to a and see what happens in print!

void main(a) {
  var a;/ / variable
  a = "this is a !";
  print(a);
  a = 100;
  print(a);
}
Copy the code

The print result is as follows:

Var declaration variable, does not specify the type, also does not specify the type, is a dynamic type, mouse will prompt type!

It also supports four operations,

void main(a) {
  var a;/ / variable
  a = "this is a !";
  print(a);
  a = 100;
  print(a);
  print(a + 10);
  print(a - 10);
  print(a * 10);
  print(a / 10);
  var b = 200;
  print(b);
}
Copy the code

The print result is as follows:

Type b: int type b: int type B: int

It can be known from the above:

  • dartThe use ofvarDeclare variables, can assign different types of value, will automatically infer the type and variableSwiftIn is the same effect.
  • varA declared variable, if not initialized, has a value of nil.

3. final

Variables declared with final can only be assigned once.

This is a bit strange, for example 🌰, the following code:

void main(a){
  final a = 10;
  a = "hello";
}
Copy the code

Run error as shown below:

Final modifies a final variable and cannot be assigned again, otherwise an error will be reported. You can also declare the assignment again first, but only once.

Final applies to declared variables, variables that do not change later, which is like a constant and can only be assigned once.

Variables declared by final cannot be used without an assignment.

4. const

Const modifies a constant and is assigned when declared. This is the biggest difference between a const and a variable.

void main(a){
  final a;
  a = 9;
  print(a);
  const b;
  b = 8;

}
Copy the code

Constant declaration does not assign a value, as follows:

Then the constant initialization assignment does not report an error and can be printed normally.

Const constants cannot be modified or reassigned, just like final.

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