[Flutter] Basic use of var, final and const in Dart

1. Write in front

In the previous article, the environment installation and configuration of [ Flutter ], as well as the solutions to some problems encountered, will be officially opened now.dartgrammar learning!
Flutter

[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

Let's first look at the most basicwas,finalandconstBar!

2nd var

look at thiswasBasically you know what it means, right or wrong,wasIt means to define variables.

void  main ( )  { 
  var a ; //variable 
  print ( a ) ; 
}
  • 1
  • 2
  • 3
  • 4

If a variable is defined but not assigned a value and the data type of the variable is not known, what will be the result of printing?

code running result
The result printed here isnull, then now giveaAssign a value to see, what will happen to printing?

void main() {
  var a;//变量
  a = "this is a !";
  print(a);
}
  • 1
  • 2
  • 3
  • 4
  • 5


print result
The print result at this time is as follows:aassignedstringIf it is printed out successfully, then assign other values ​​to a, and see when printing!

void main() {
  var a;//变量
  a = "this is a !";
  print(a);
  a = 100;
  print(a);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

The print result is as follows:

print result
wasThe declared variable has no specified type and no specified type. It is a dynamic type, and the type will be prompted when the mouse is placed on it!

Put the mouse on the variable name, prompt the type
It also supports four arithmetic operations,

void main() {
  var a;//变量
  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);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

The print result is as follows:
print result
herebassignednumber, the type is determined, and the mouse hovers to see that it isinttype!
Mouse hover display type
From the above it can be known that:

  • dartused inwasDeclare a variable, you can assign values ​​of different types, the type of the variable andSwiftis the same effect.
  • wasIf a declared variable is not initialized, its value isnil.

3. final

usefinalA declared variable, which can only be assigned a value once.

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

void main(){
  final a = 10;
  a = "hello";
}
  • 1
  • 2
  • 3
  • 4

An error is reported when running, as follows:
run error
finalModified is a final variable and cannot be assigned again, otherwise an error will be reported. You can also declare and assign again, but you can only assign it once.
operation result

finalIt is applicable to declared variables and variables that will not be changed in the future, which is similar to the meaning of a constant, which can only be assigned a value once.
finalA declared variable cannot be used without assignment.

Error when using without assignment

4. const

constWhen modifying constants, you have to assign values ​​when you declare them. This is also the biggest difference from variables!

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

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

If the constant declaration does not assign a value, an error will be reported, as follows:
error message
then the constant initialization assignment will not report an error, and the result can be output normally.

output result
constConstants are also unmodifiable and cannot be assigned again. This is the same asfinalSame.
error message

5. Write it down

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,ForwardPlease indicate the source, thank you for your support! 🌹

Tags: [Flutter] Basic use of var, final and const in Dart

Flutter flutter android ios dart Cross-platform

Related: [Flutter] Basic use of var, final and const in Dart