5) 使用庫(kù) 引入 按需加載
其中 pag.Calc.dart
文件內(nèi)容,簡(jiǎn)單的 相加求和 ,還有一個(gè)相減求差轧拄。
int add(int x,int y){
return x+y;
}
class Calc {
int x;
int y;
Calc(int x , int y){
this.x = x;
this.y = y;
}
minus(){
print(this.x-this.y);
}
}
主程序如下:
import 'dart:convert';
//import 'dart:math';
import 'pkg/Calc.dart'; //引入
import 'package:http/http.dart' as http; //調(diào)用外部的包
// deferred 延時(shí)加載 使用時(shí) 加載
import 'dart:math' deferred as math; //調(diào)用dart自身的包
void main() async {
int start = 1;
int count = 2;
int result = add(2, 4);
print(result); // 6
var m = new Calc(5,85);
m.minus(); //-80
//簡(jiǎn)單json序列化
//官方↓↓↓↓↓↓↓
//JSON.decode()僅返回一個(gè)Map<String, dynamic>汰具,這意味著我們直到運(yùn)行時(shí)才知道值的類(lèi)型。
// 通過(guò)這種方法粥烁,我們失去了大部分靜態(tài)類(lèi)型語(yǔ)言特性:類(lèi)型安全贤笆、自動(dòng)補(bǔ)全和最重要的編譯時(shí)異常。
// 這樣一來(lái)讨阻,我們的代碼可能會(huì)變得非常容易出錯(cuò)
//簡(jiǎn)而言之 就像js處理數(shù)據(jù)一樣不夠嚴(yán)謹(jǐn) 沒(méi)有數(shù)據(jù)類(lèi)型 TS就是為了解決這個(gè)問(wèn)題
var json1 = '''
{
"name": "John Smith",
"email": "john@example.com"
}
''';
Map<String,dynamic> jsonp1 = json.decode(json1);
assert(jsonp1 is Map);
print("hello,${jsonp1["name"]}"); //hello,John Smith
print("email,${jsonp1["email"]}"); //email,john@example.com
// 模型類(lèi)中序列化JSON
var json2 = '''
{
"name": "John2 Smith",
"email": "john2@example.com"
}
''';
Map userMap = json.decode(json2);
var jsonp2 = new Jsonp2.fromJson(userMap);
print("hello,${jsonp2.name}"); //hello,John2 Smith
print("email,${jsonp2.email}"); //email,john2@example.com
// ---------------------------------
var url = 'https://douban.uieee.com/v2/movie/top250?start=${start}&count=${count}';
var response = await http.get(url);
print('Response status: ${response.statusCode}'); //Response status: 200
// print('Response body: ${response.body}');
var encoded = json.decode(response.body.toString());//json解析 轉(zhuǎn)對(duì)象 需引入 'dart:convert';
print(encoded); //打印數(shù)據(jù)
print(await http.read('https://www.baidu.com')); //返回百度 html格式
// ------------------------------------
// 引入了math庫(kù)
math.loadLibrary(); //用于延時(shí)加載 使用時(shí) 告知需加載 按需加載
var random = new math.Random();
print(random.nextInt(10));
}
//json解析成實(shí)例對(duì)象
class Jsonp2 {
final String name;
final String email;
Jsonp2(this.name,this.email);
Jsonp2.fromJson(Map<String,dynamic> json):name = json["name"],email = json["email"];
Map<String,dynamic> toJson()=>{
'name':name,
'email':email
};
}
6) 異步
因?yàn)镈art是單線(xiàn)程芥永,所以代碼在運(yùn)行線(xiàn)程中阻塞的話(huà),會(huì)使程序凍結(jié)
钝吮,在Dart中Future
表示異步操作的結(jié)果,異步操作可以允許程序在等待操作完成期間可以去完成其他工作埋涧。
最基本的異步表現(xiàn):
void main(){
print('say hello');
Future.delayed(new Duration(seconds: 5),(){
print('吃飽了--這是異步');
});
print('play game');
/*
say hello
play game
//5秒后↓
吃飽了--這是異步
*/
}
異步操作 同步調(diào)用 async...await
void main() async {
print('say hello');
await Future.delayed(new Duration(seconds: 5),(){
print('吃飽了--這是異步,但同步調(diào)用');
});
print('play game');
Future.wait([
//異步 程序同時(shí)進(jìn)行
Future.delayed(new Duration(seconds: 1),(){
print('異步001');
}),
Future.delayed(new Duration(seconds: 2),(){
print('異步002');
}),
Future.delayed(new Duration(seconds: 5),(){
print('異步003');
}),
Future.delayed(new Duration(seconds: 4),(){
print('異步004');
}),
]).then((List result){ //then 同js中then await +異步 執(zhí)行結(jié)束后 執(zhí)行
// 在async函數(shù)中使用try-catch來(lái)捕獲異常(或者使用catchError())
print('異步all over');
});
// 要想同時(shí)運(yùn)行代碼,就要為一個(gè)web app或者一個(gè)worker創(chuàng)建一個(gè)isolate
//所有Dart代碼都在一個(gè)擁有Dart代碼使用的所有內(nèi)存的isolate上下文中運(yùn)行奇瘦。Dart代碼在運(yùn)行的時(shí)候棘催,在同一隔離中的其他代碼不能運(yùn)行。
//如果你希望Dart代碼的多個(gè)部分同時(shí)運(yùn)行耳标,你可以將它們?cè)诓煌膇solate中運(yùn)行(Weba pps使用workers代替isolates)巧鸭。多個(gè)isolate同時(shí)運(yùn)行,通常是各自運(yùn)行在各自的CPU上麻捻。isolate不共享內(nèi)存纲仍,它們可以交互的唯一方式就是互相發(fā)送消息呀袱。
//同步寫(xiě)法 此時(shí) 時(shí)間需累加 時(shí)間變長(zhǎng)
await Future.delayed(new Duration(seconds: 1),(){
print('005');
});
await Future.delayed(new Duration(seconds: 2),(){
print('006');
});
await Future.delayed(new Duration(seconds: 5),(){
print('007');
});
await Future.delayed(new Duration(seconds: 4),(){
print('008');
});
/*
say hello
吃飽了--這是異步,但同步調(diào)用
play game
異步001
005
異步002
006
異步004
異步003
異步all over
007
008
*/
}
太長(zhǎng)啦,還不支持錨點(diǎn)郑叠,所以拆成三篇