Introduction
Flutter is an open source mobile UI construction framework launched by Google at the 2015 Dart Developer Summit. Using flutter can be very convenient to compile into mobile applications running on the original mobile platforms such as android, ios, and web.
Flutter is written using dart, the latest flutter version is 2.5.3, and the latest Dart language version is 2.14.
This series will talk in depth about the usage and best practices of the dart language, I hope you will enjoy it.
Variables in dart
Dart language absorbs the essence of java and javascript. If you are a developer of the above languages, it will be easy to migrate to the dart language. Let's start with the most basic variables of a language and explore the mysteries of the dart language.
Define variables
Variables defined in Dart are the same as those defined in java and javascript, as shown below:
var name = 'jack';
Above we use var to indicate that the type of name can be inferred. In the process of programming, if we encounter some variables and don't know their types, we can try to use var and let dart infer it by itself.
Of course, we can also specify the type of the variable, as shown above, you can specify the name as String type:
String name = 'jack';
In dart, all variables are Object, and each object is an instance of Class. Including numbers, functions, and null are all Objects. All objects inherit from the Object class.
So the above assignment can also be written like this:
Object name = 'jack';
The default value of the variable
In dart, uninitialized variables have a value of nullable type, and the initial value of this value is null.
Unlike in java, the initial value of all numbers in dart is also null. This is because the number in dart is also an object.
If a variable can be null, you can add? After the variable type when assigning a value, as shown below:
int? age;
For class variables, they will only be initialized when they are used. This initialization strategy is called delayed initialization.
Late variables
The Late modifier is a new feature introduced in Dart 2.12. It can indicate that the variable needs to be loaded delayed, or that a variable that is not empty will be initialized later.
We can use it like this:
late int age;
void main() {
age = 18;
print(age);
}
Why use late? Because sometimes Dart cannot check whether certain variables are initialized before use, but if you are very sure, use late to modify it.
In addition, variables modified by late are initialized only when they are used, so we can use late to define some time-consuming and resource-consuming operations.
constant
If the variable does not change, then this is not a variable, but a constant.
Constants can be modified with final or const. A final variable means that the variable will only be assigned once.
The const variable means that the variable will be assigned at compile time, and the default const is also final.
As follows:
final age = 18;
final int age = 18;
const age = 20;
If the const variable is a class variable, set it to static.
Constant can also be used for assignment, as shown below:
var age = const [];
final bar = const [];
const baz = []; // Equivalent to `const []`
In the above code, although the value of age is const, age itself is not const, so age can be re-assigned:
foo = [18, 21, 23];
But bar is final, so bar cannot be reassigned.
Summarize
The above is the use of variables in the dart language.
This article has been included in http://www.flydean.com/01-dart-variables/
The most popular interpretation, the most profound dry goods, the most concise tutorial, and many tips you don't know are waiting for you to discover!
Welcome to pay attention to my official account: "Program those things", know technology, know you better!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。