[Flutter 1-7] Flutter tutorial Dart language-variables

Author | Vlad
Source | Vlad (Public Account: fulade_me)

At the GOTO conference on October 10, 2011, two Google engineers released "Dart"; Dart is a new programming language designed to help developers build web applications. Dart 1.0 was released on November 14, 2013. We use the Dart language to develop Flutter every day, so it is necessary for us to understand how to use the Dart language.
Article starting address

Type-safe language

The Dart language is a type-safe language, but because it supports type inference, most variables do not need to be explicitly typed:
for example

/// 初始化一个字符串
var name = 'Fulade';
/// Int类型
var year = 1995;
/// 浮点数类型
var antennaDiameter = 3.7;
/// 数组
var list = ['Java', 'Python', 'C++', 'C'];
/// 字典类型
var image = {
'tags': ['土星'],
'url': '//path/to/saturn.jpg'
};

variable

The following sample code will create a variable and initialize it:

var name = 'Fulade';

Variables only store references to objects.
Where the variable named name stores a Stringreference type object, 'Fulade'is the value of the object.
nameThe type of the variable is inferred String, but you can specify the type for it.
If an object reference is not limited to a single type can be designated as a Objector dynamictype.

dynamic name = 'Bob';

In addition, you can also specify the type:

String name = 'Bob';

Defaults

In the Dart, uninitialized variables have a default initialization value: null. This is true even with numbers, because everything is an object in Dart, and numbers are no exception.

int lineCount;
if(lineCount == null) {
print("line is null");
}
Final and Const

If you do not want to change a variable, you can use the keyword finalor constmodify variables, which can replace the two keywords varkeywords or add in front of a specific type. A finalvariable can be assigned only once; a constvariable is a compile-time constants ( constvariables also final). The finalmodified variable is initialized the first time it is used.
The following example, we create and set up two finalvariables:

final name = 'Bob'; // Without a type annotation
final String nickname = 'Bobby';

You can not change a finalvalue of a variable:

name = 'Alice'; // Error: a final variable can only be set once.

Use the keyword constmodified variable represents the variable compile-time constant . If you use the constmodified class variables, you must add statickeywords, namely static const(Note: The order can not be reversed in a statement. constCan directly assign a value to a variable time, you can also use other constvariables for their assignment:

const bar = 1000000; // 直接赋值 [Unit of pressure (dynes/cm2)]
const double atm = 1.01325 * bar; // 利用其它 const 变量赋值 (Standard atmosphere)

constKeywords can be used not only to define constants, but also to create constant values, which can be assigned to any variable. You can also declare a constructor const, the object constructor creates this type is immutable.

var foo = const [];
final bar = const [];
const baz = []; // 相当于 `const []` (Equivalent to `const []`)

If you use the initialization expression for the constant values may be omitted keyword const, such as constant above bazassigned to omitted constno use finalor constvalue of a variable that can be modified to change, even if quoted before these variables constvalue.

foo = [1, 2, 3]; // foo 的值之前为 const [] (Was const [])

The value of a constant cannot be modified:

baz = [42]; // 报错:常量不可以被赋值。(Error: Constant variables can't be assigned a value.)

the public

Guess you like

Origin blog.51cto.com/13824996/2551359