var
類似在JavaScript中一樣,你可以使用var關鍵字定義變量
main(List<String> args) {
var number = 42;
var name = 'Gurleen Sethi';
var salary = 150300.56;
var isDoorOpen = true;
}
但是,和JavaScript不同的是,在Dart2中,一旦你給變量賦值一種類型的值,就不能再賦值另一種類型的值。Dart 可以自動從右邊數據推斷數據類型。
特殊情況
Dart2.1裏面新增特性,當double的值爲int值時,int自動轉成double。
例如:double test = 12;//打印結果是12.0
即 var a=5.5 ,確定是變量a是double類型時,也可以接受a=10;
Dart2.1,double也有api轉成int,會把小數點後面的全部去掉。
double test2 = 15.1;
double test3 = 15.1234;
print(test2.toInt());// 結果是15
print(test3.toInt());// 結果是15
Final 和 Const的用法
1、被final或者const修飾的變量,變量類型可以省略。
//可以省略String這個類型聲明
final name1 = "張三";
//final String name1 = "張三";
const name2 = "李四";
//const String name2 = "李四";
2、被 final 或 const 修飾的變量無法再去修改其值。
3、注意:flnal 或者 const 不能和 var 同時使用,
//這樣寫都會報錯
//final var name1 = "張三";
//const var name2 = "李四";
Lists
main(List<String> args) {
var list = [1,2,3,4];
print(list); //Output: [1, 2, 3, 4]
//Length 長度
print(list.length);
//Selecting single value 獲取單個值
print(list[1]); //Outout: 2
//Adding a value 添加值到list
list.add(10);
//Removing a single isntance of value 刪除單個值
list.remove(3);
//Remove at a particular position 刪除指定位置的值
list.removeAt(0);
var list = const [1,2,3,4]; //不可變
}
Maps
main(List<String> args) {
var map = {
'key1': 'value1',
'key2': 'value2',
'key3': 'value3'
};
//Fetching the values 獲取值
print(map['key1']); //Output: value1
print(map['test']); //Output: null
//Add a new value 添加值
map['key4'] = 'value4';
//Length 獲取長度
print(map.length);
//Check if a key is present 檢查是否存在
map.containsKey('value1');
//Get entries and values
var entries = map.entries;
var values = map.values;
}
異常
如果你不知道拋出異常的類型,或者不確定,可以使用catch塊處理任意類型的異常。
main(List<String> args) {
try {
divide(10, 0);
} on IntegerDivisionByZeroException {
print('Division by zero.');
} catch (e) {
print(e);
}
}
divide(int a, int b) {
if (b == 0) {
throw new Exception('Some other exception.');
}
return a / b;
}
構造函數的寫法
給構造函數提供了名稱,這樣做使得不同的構造函數變的更加清晰。
class Dog {
String name;
int age;
Dog(this.name, this.age);
Dog.newBorn() {
name = 'Doggy';
age = 0;
}
}
Getter and Setters
main(List<String> args) {
Dog d = new Dog('Duffy', 5);
d.respectedName = 'Mr.Duffy';
print(d.respectedName);
}
class Dog {
String name;
int age;
Dog(this.name, this.age);
String get respectedName {
return 'Mr.$name';
}
set respectedName(String newName) {
name = newName;
}
Dog.newBorn() {
name = 'Doggy';
age = 0;
}
bark() {
print('Bow Wow');
}
}
訪問控制
在dart中,可以在屬性和方法名前添加“_”使私有化。現在讓我們使name屬性私有化。
枚舉
main(List<String> args) {
Dog d = new Dog('Duffy', 12, CurrentState.sleeping);
print(d.state == CurrentState.sleeping); //Prints 'true'
}
enum CurrentState {
sleeping,
barking,
eating,
walking
}
class Dog {
String name;
int age;
CurrentState state;
Dog(this.name, this.age, this.state);
static bark() {
print('Bow Wow');
}
}
as, is, is!
as,is,和 !is 運算符在運行時檢查類型很方便
as: 類型轉換, 也用於指定庫前綴
is: 類似於java的instanceof
!is: is操作符的取反, 即不是xxx
代碼示例:
if (emp is Person) {
// 類型檢查
emp.firstName = 'Bob';
}
// 如果emp爲Person,則將firstName改爲Bod, 否則會在運行時期報錯
(emp as Person).firstName = 'Bob';