學習筆記,旨在于快速入門和學習Dart,其中可能會有理解錯誤,請指出,一起學習。
系列文章
2.1、Dart語言基礎(chǔ):變量、運算符
2.2、Dart語言基礎(chǔ):函數(shù)與閉包
2.3、Dart語言基礎(chǔ):面向?qū)ο?/a>
2.4、Dart語言基礎(chǔ):異步
2.5、Dart語言基礎(chǔ):庫與包
...
一、概述
- 語法類似C語言,可以轉(zhuǎn)譯為JavaScript;
- 面向?qū)ο笳Z言,屬于強類型語言,支持可選類型;
參考:
dart英文官網(wǎng)
Dart 中文教程
dart-源碼api
特性
重要概念
一切皆為對象
包括function函數(shù)、基礎(chǔ)變量(int、float等)、null等;而且?guī)缀跛蓄惗祭^承于Object基類,Null除外。強類型語言,支持類型推導。
強類型指的是不能給變量賦值類型不匹配的值,如下:int類型的賦值字符串報錯。
var age = 10; // 類型推導為int
age = "xxx"; // 給int類型的變量 賦值 字符串報錯
print(age);
- 支持可選類型
空值安全type?(類似于swift的可選類型),指的是變量可能為null。
int? aNullableInt = null;
如果變量確定一定有值,可以使用!(如果為空,則會拋出異常)。
int x = nullableButNotNullInt!
- 字符串插值:
美元符號$varName 或者 ${varName}
void sayHelloworld(String name) {
print("$name say hello!"); //${name}
}
- 頂級入口
main函數(shù)。
void main(List<String> arguments) { ... }
- 定義任何類型的變量:
Object?、Object或者 關(guān)鍵字dynamic(在程序運行時才確定類型)。
二、基礎(chǔ)
0、基本格式
代碼語句,必須以 分號
;結(jié)尾。代碼注釋:
單行語句// xxx
多行語句/* xxx */類型安全檢測:
Dart 的類型安全意味著不能使用if / assert, 應該像下面這樣,明確的進行值檢查:
// 檢查空字符串。
var fullName = '';
assert(fullName.isEmpty);
// 檢查 0 值。
var hitPoints = 0;
assert(hitPoints <= 0);
// 檢查 null 值。
var unicorn;
assert(unicorn == null);
// 檢查 NaN 。
var iMeantToDoThis = 0 / 0;
assert(iMeantToDoThis.isNaN);
1、變量與常量
1.1、變量
Dart萬物結(jié)尾對象,因此變量實際就是一個引用。
1.1.1、聲明方式
方式一:明確指定類型
String name = 'hello';方式二:不指定類型,關(guān)鍵字
var
官方推薦在函數(shù)內(nèi)的本地變量盡量使用var聲明。
var name = 'hello';方式三:不指定類型,關(guān)鍵字
dynamic
在變量類型,不明確的情況下使用。
dynamic name = 'hello';方式四:
?明確指定可為空的變量
int? aNullableAge;
1.1.2、變量聲明是否可以為空?
- 指定類型的變量,聲明必須初始化,”不為空“,否則編譯報錯。
// Error: Non-nullable variable 'obj' must be assigned before it can be used.
Object obj;
print(obj);
// Error: Non-nullable variable 'nickName' must be assigned before it can be used.
String nickName;
print(nickName);
// Error: Non-nullable variable 'age' must be assigned before it can be used.
int age;
print(age);
- 可為空(
dynamic、var或者?聲明)的變量,初始化可以為空;
// 初始化可以不指定類型,值為null
dynamic nickName;
print(nickName);
var name;
print(name);
String? nickName1;
print(nickName1);
1.1.3、延遲變量,關(guān)鍵字late(Dart 1.6新加)
- 定義一個允許延遲初始化的變量。
// late的特性:
Lazily initializing a variable.
Declaring a non-nullable variable that’s initialized after its declaration.
// late的使用場景
This lazy initialization is handy in a couple of cases:
1、The variable might not be needed, and initializing it is costly.
2、You’re initializing an instance variable, and its initializer needs access to this.
late String description;
void main() {
description = 'Feijoada!';
print(description);
}
- 如果
延遲變量未被其他地方使用,對應語句不執(zhí)行。
// 如果temperature沒有被使用,則函數(shù) _readThermometer 不會調(diào)用
late String temperature = _readThermometer();
1.2、常量
- 關(guān)鍵字
final, 變量的值只能被設置一次。
最高級 final 變量或類變量在第一次使用時被初始化。
必須在構(gòu)造函數(shù)體執(zhí)行之前初始化 final 實例變量 —— 在變量聲明中,參數(shù)構(gòu)造函數(shù)中或構(gòu)造函數(shù)的初始化列表中進行初始化。
// Error: Can't assign to the final variable 'name'.
final String name = 'hello';
name = 'new hello';
- 關(guān)鍵字
const, 變量在編譯時就已經(jīng)固定。
如果 Const 變量是類級別的,需要標記為 static const。
實例變量可以是 final 類型但不能是 const 類型。
// Error: Can't assign to the const variable 'name'.
const String name = 'hello';
name = 'new hello';
1.2.1、變量 以及 變量本身 均不允許被修改
// Error: Can't assign to the const variable 'list'.
const list = [1, 2, 3];
list = [11, 12];
// Unsupported operation: Cannot modify an unmodifiable list
list[0] = 10;
print(list);
- 以下不會報錯
// 此時,list推導的類型為不可變數(shù)組,修改字典值報錯;
var list = const [1,2,3];
list[0] = 10; // 報錯Unsupported operation: Cannot modify an unmodifiable list
// 但是變量list可以被重新初始化賦值
list = [1, 2, 3];
print(list);
list[0] = 10;
print(list);
3、內(nèi)置類型
3.1、專有變量

3.2、數(shù)值類型,關(guān)鍵字num,可以是int和double
num val = 1;
print(val); // 1
val += 0.5;
print(val); // 1.5
- 布爾值,關(guān)鍵字
false和true
bool flag = false;
print(flag);
flag = true;
print(flag);
3.3、數(shù)組,關(guān)鍵字List
var list = [1, 2, 3];
list[0] = 0;
list.length;
// C語言,大括號
int arr1[3] = {1, 2, 3};
常用APIs:
void testListTypeDemo() {
var fruits = [];
print('isEmpty:${fruits.isEmpty}');
fruits.add('apples');
fruits.addAll(['pear', 'banana']);
print('原數(shù)組:$fruits');
print('first:${fruits.first}, last:${fruits.last}');
print('length:${fruits.length}');
print('indexOf: ${fruits.indexOf('banana')}');
print("contain:${fruits.contains('apples')}");
print("join:${fruits.join('、 ')}");
fruits.insert(0, 'watermalon');
print('原數(shù)組:$fruits');
print('range:${fruits.getRange(0, 2)}');
fruits.remove('apples');
fruits.removeAt(0);
print(fruits);
fruits.clear();
print(fruits);
var list = List.filled(8, 'name');
print(list);
var numbers = [1, 2, 3, 4, 5, 6, 7, 8];
print('原數(shù)組:$numbers');
numbers.sort((a, b) => a>b?a:b);
print('倒敘:$numbers');
}
- 展開運算符
spread operator (...) 和 null-aware spread operator (...?)
var list = [1, 2, 3];
var list2 = [0, ...list];
print(list2); // [0, 1, 2, 3]
var null_list;
var list3 = [0, ...?null_list]; // 可為空
print(list3); // [0]
-
collection if和collection for
Dart also offers collection if and collection for, which you can use to build collections using conditionals (if) and repetition (for).
bool promoActive = true;
var nav = ['Home', 'Furniture', 'Plants', if (promoActive) 'Outlet'];
print(nav); // [Home, Furniture, Plants, Outlet]
var listOfInts = [1, 2, 3];
var listOfStrings = ['#0', for (var i in listOfInts) '#$i'];
print(listOfStrings); // [#0, #1, #2, #3]
3.4、集合,關(guān)鍵字Set,(無序,大括號定義)
var halogens = {'fluorine', 'chlorine', 'bromine', 'iodine', 'astatine'};
var elements = <String>{};
elements.add('fluorine');
elements.addAll(halogens);
- 在 Set 字面量前增加 const ,來創(chuàng)建一個編譯時 Set 常量:
final constantSet = const {
'fluorine',
'chlorine',
'bromine',
'iodine',
'astatine',
};
3.5、字典,關(guān)鍵字Map
// Map<String, String>
var gifts = {
// Key: Value
'first': 'partridge',
'second': 'turtledoves',
'fifth': 'golden rings'
};
3.6、標識符,Symbol
# label_1
# label_x
4、流程控制
-
if/else(與C語言基本一樣)
if (cond1) {
} else if (cond2) {
} else {
}
-
for:與C語言基本類似,支持for-in,
for (int i=0; i<8; i++) {
print(i);
}
// for-in
for (var object in flybyObjects) {
print(object);
}
-
while / do-while(與C語言基本一樣)
while(1) {
}
do {
} while(1)
break/continue(與C語言基本一樣)在生產(chǎn)環(huán)境代碼中
assert()函數(shù)會被忽略,不會被調(diào)用。
在開發(fā)過程中,assert(condition)會在非 true 的條件下拋出異常。
switch/case
- 使用
==比較整數(shù),字符串,或者編譯時常量。 - 比較的對象必須都是同一個類的實例,并且不可以是子類, 類必須沒有對
==重寫。 - 非空的
case語句結(jié)尾需要跟一個 break 語句???case 語句, 允許程序以fall-through的形式執(zhí)行。
var command = 'CLOSED';
switch (command) {
case 'OPEN': //非空case,必須跟break
executeOpen();
break;
case 'CLOSED': // 空 case ,支持falls-through.
case 'NOW_CLOSED':
// Runs for both CLOSED and NOW_CLOSED.
executeNowClosed();
break;
}
- 支持
lable
var command = 'CLOSED';
switch (command) {
case 'CLOSED':
executeClosed();
continue nowClosed;
// Continues executing at the nowClosed label.
nowClosed:
case 'NOW_CLOSED':
// Runs for both CLOSED and NOW_CLOSED.
executeNowClosed();
break;
}
5、運算符
5.1、基本
- 除法:
~/ 和 /
assert(5 / 2 == 2.5); // 結(jié)果是雙浮點型
assert(5 ~/ 2 == 2); // 結(jié)果是整型
- 支持自增自減:
++a、a++、--a、a--
5.2、空合運算符??
-
expr1 ?? expr2
如果 expr1 是 non-null, 返回 expr1 的值; 否則, 執(zhí)行并返回 expr2 的值。
// 如果b為空時,將變量賦值給b,否則,b的值保持不變。
b ??= value;
5.3、類型判定運算符
-
as將對象強制轉(zhuǎn)換為特定類型。 -
is 和 is!判斷變量是不是某種類型。
5.4、級聯(lián)運算符 ..
可以實現(xiàn)對同一個對像進行一系列的操作。
querySelector('#confirm') // 獲取對象。
..text = 'Confirm' // 調(diào)用成員變量。
..classes.add('important')
..onClick.listen((e) => window.alert('Confirmed!'));
// 等價于
var button = querySelector('#confirm');
button.text = 'Confirm';
button.classes.add('important');
button.onClick.listen((e) => window.alert('Confirmed!'));