1.var 賦值后類型確定
2.Object 是Dart所有對象的根基類,也就是說所有類型都是Object的子類(包括Function和Null)
3.dynamic的這個特性與Objective-C中的id作用很像.
4.Dart是一種真正的面向?qū)ο蟮恼Z言,所以即使是函數(shù)也是對象订歪,并且有一個類型Function恶迈。這意味著函數(shù)可以賦值給變量或作為參數(shù)傳遞給其他函數(shù),這是函數(shù)式編程的典型特征吠昭。
5.包裝一組函數(shù)參數(shù)英上,用[]標(biāo)記為可選的位置參數(shù),并放在參數(shù)列表的最后面
6.注意罚斗,不能同時使用可選的位置參數(shù)和可選的命名參數(shù)
7.Future與JavaScript中的Promise非常相似
// 延遲執(zhí)行
Future.delayed(new Duration(seconds: 2),(){
//return "hi world!";
throw AssertionError("Error");
}).then((data){
//執(zhí)行成功會走到這里
print(data);
}).catchError((e){
//執(zhí)行失敗會走到這里
print(e);
}).whenComplete((){
//無論成功或失敗都會走到這里
});
// 同步
Future.wait([
// 2秒后返回結(jié)果
Future.delayed(new Duration(seconds: 2), () {
return "hello";
}),
// 4秒后返回結(jié)果
Future.delayed(new Duration(seconds: 4), () {
return " world";
})
]).then((results){
print(results[0]+results[1]);
}).catchError((e){
print(e);
});
8.使用async/await消除callback hell
task() async {
try{
String id = await login("alice","******");
String userInfo = await getUserInfo(id);
await saveUserInfo(userInfo);
//執(zhí)行接下來的操作
} catch(e){
//錯誤處理
print(e);
}
}
9.Stream 常用于會多次讀取數(shù)據(jù)的異步任務(wù)場景徙鱼,如網(wǎng)絡(luò)內(nèi)容下載、文件讀寫等
Stream.fromFutures([
// 1秒后返回結(jié)果
Future.delayed(new Duration(seconds: 1), () {
return "hello 1";
}),
// 拋出一個異常
Future.delayed(new Duration(seconds: 2),(){
throw AssertionError("Error");
}),
// 3秒后返回結(jié)果
Future.delayed(new Duration(seconds: 3), () {
return "hello 3";
})
]).listen((data){
print(data);
}, onError: (e){
print(e.message);
},onDone: (){
});