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 addressType-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 aString
reference type object,'Fulade'
is the value of the object.name
The type of the variable is inferredString
, but you can specify the type for it.
If an object reference is not limited to a single type can be designated as aObject
ordynamic
type.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
final
orconst
modify variables, which can replace the two keywordsvar
keywords or add in front of a specific type. Afinal
variable can be assigned only once; aconst
variable is a compile-time constants (const
variables alsofinal
). Thefinal
modified variable is initialized the first time it is used.
The following example, we create and set up twofinal
variables:final name = 'Bob'; // Without a type annotation final String nickname = 'Bobby';
You can not change a
final
value of a variable:name = 'Alice'; // Error: a final variable can only be set once.
Use the keyword
const
modified variable represents the variable compile-time constant . If you use theconst
modified class variables, you must addstatic
keywords, namelystatic const
(Note: The order can not be reversed in a statement.const
Can directly assign a value to a variable time, you can also use otherconst
variables for their assignment:const bar = 1000000; // 直接赋值 [Unit of pressure (dynes/cm2)] const double atm = 1.01325 * bar; // 利用其它 const 变量赋值 (Standard atmosphere)
const
Keywords 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 constructorconst
, 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 abovebaz
assigned to omittedconst
no usefinal
orconst
value of a variable that can be modified to change, even if quoted before these variablesconst
value.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.)