摘自:https://www.cnblogs.com/loaderman/p/11027076.html
前面介紹Dart基礎(chǔ)知識(shí)的時(shí)候基本上都是在一個(gè)文件里面編寫Dart代碼的,但實(shí)際開發(fā)中不可能這么寫蜈出,模塊化很重要,所以這就需要使用到庫(kù)的概念邮绿。
在Dart中么介,庫(kù)的使用時(shí)通過(guò)import關(guān)鍵字引入的。
library指令可以創(chuàng)建一個(gè)庫(kù),每個(gè)Dart文件都是一個(gè)庫(kù)褐着,即使沒有使用library指令來(lái)指定判莉。
Dart中的庫(kù)主要有三種:
1豆挽、我們自定義的庫(kù)
import 'lib/xxx.dart';
2、系統(tǒng)內(nèi)置庫(kù)
import 'dart:math';
import 'dart:io';
import 'dart:convert';
3券盅、Pub包管理系統(tǒng)中的庫(kù)
https://pub.dev/packages
https://pub.flutter-io.cn/packages
https://pub.dartlang.org/flutter/
1帮哈、需要在自己想項(xiàng)目根目錄新建一個(gè)pubspec.yaml
2、在pubspec.yaml文件 然后配置名稱 锰镀、描述娘侍、依賴等信息
image.png
3、然后運(yùn)行 pub get 獲取包下載到本地
4泳炉、項(xiàng)目中引入庫(kù) import 'package:http/http.dart' as http; 看文檔使用
導(dǎo)入本地庫(kù)
import 'lib/Animal.dart';
main(){
var a=new Animal('小黑狗', 20);
print(a.getName());
}
class Animal{
String _name; //私有屬性
int age;
//默認(rèn)構(gòu)造函數(shù)的簡(jiǎn)寫
Animal(this._name,this.age);
void printInfo(){
print("${this._name}----${this.age}");
}
String getName(){
return this._name;
}
void _run(){
print('這是一個(gè)私有方法');
}
execRun(){
this._run(); //類里面方法的相互調(diào)用
}
}
導(dǎo)入系統(tǒng)內(nèi)置的庫(kù)
// import 'dart:io';
import "dart:math";
main(){
print(min(12,23));
print(max(12,25));
}
import 'dart:io';
import 'dart:convert';
void main() async{
var result = await getDataFromZhihuAPI();
print(result);
}
//api接口: http://news-at.zhihu.com/api/3/stories/latest
getDataFromZhihuAPI() async{
//1憾筏、創(chuàng)建HttpClient對(duì)象
var httpClient = new HttpClient();
//2、創(chuàng)建Uri對(duì)象
var uri = new Uri.http('news-at.zhihu.com','/api/3/stories/latest');
//3花鹅、發(fā)起請(qǐng)求氧腰,等待請(qǐng)求
var request = await httpClient.getUrl(uri);
//4、關(guān)閉請(qǐng)求刨肃,等待響應(yīng)
var response = await request.close();
//5古拴、解碼響應(yīng)的內(nèi)容
return await response.transform(utf8.decoder).join();
}
說(shuō)明
/*
async和await
這兩個(gè)關(guān)鍵字的使用只需要記住兩點(diǎn):
只有async方法才能使用await關(guān)鍵字調(diào)用方法
如果調(diào)用別的async方法必須使用await關(guān)鍵字
async是讓方法變成異步。
await是等待異步方法執(zhí)行完成真友。
*/
void main() async{
var result = await testAsync();
print(result);
}
//異步方法
testAsync() async{
return 'Hello async';
}
導(dǎo)入第三方庫(kù)
/*
pub包管理系統(tǒng):
1黄痪、從下面網(wǎng)址找到要用的庫(kù)
https://pub.dev/packages
https://pub.flutter-io.cn/packages
https://pub.dartlang.org/flutter/
2、創(chuàng)建一個(gè)pubspec.yaml文件盔然,內(nèi)容如下
name: xxx
description: A new flutter module project.
dependencies:
http: ^0.12.0+2
date_format: ^1.0.6
3满力、配置dependencies
4、運(yùn)行pub get 獲取遠(yuǎn)程庫(kù)
5轻纪、看文檔引入庫(kù)使用
*/
import 'dart:convert' as convert;
import 'package:http/http.dart' as http;
import 'package:date_format/date_format.dart';
main() async {
// var url = "http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20&page=1";
// // Await the http get response, then decode the json-formatted responce.
// var response = await http.get(url);
// if (response.statusCode == 200) {
// var jsonResponse = convert.jsonDecode(response.body);
// print(jsonResponse);
// } else {
// print("Request failed with status: ${response.statusCode}.");
// }
print(formatDate(DateTime(1989, 2, 21), [yyyy, '*', mm, '*', dd]));
}
Dart庫(kù)的重命名 Dart沖突解決
/*
1油额、沖突解決
當(dāng)引入兩個(gè)庫(kù)中有相同名稱標(biāo)識(shí)符的時(shí)候,如果是java通常我們通過(guò)寫上完整的包名路徑來(lái)指定使用的具體標(biāo)識(shí)符刻帚,甚至不用import都可以潦嘶,但是Dart里面是必須import的。當(dāng)沖突的時(shí)候崇众,可以使用as關(guān)鍵字來(lái)指定庫(kù)的前綴掂僵。如下例子所示:
import 'package:lib1/lib1.dart';
import 'package:lib2/lib2.dart' as lib2;
Element element1 = new Element(); // Uses Element from lib1.
lib2.Element element2 = new lib2.Element(); // Uses Element from lib2.
*/
import 'lib/Person1.dart';
import 'lib/Person2.dart' as lib;
main(List<String> args) {
Person p1=new Person('張三', 20);
p1.printInfo();
lib.Person p2=new lib.Person('李四', 20);
p2.printInfo();
}
部分導(dǎo)入
/*
部分導(dǎo)入
如果只需要導(dǎo)入庫(kù)的一部分航厚,有兩種模式:
模式一:只導(dǎo)入需要的部分,使用show關(guān)鍵字锰蓬,如下例子所示:
import 'package:lib1/lib1.dart' show foo;
模式二:隱藏不需要的部分幔睬,使用hide關(guān)鍵字,如下例子所示:
import 'package:lib2/lib2.dart' hide foo;
*/
// import 'lib/myMath.dart' show getAge;
import 'lib/myMath.dart' ;
void main(){
getName();
}
延遲加載
/*
延遲加載
也稱為懶加載芹扭,可以在需要的時(shí)候再進(jìn)行加載麻顶。
懶加載的最大好處是可以減少APP的啟動(dòng)時(shí)間。
懶加載使用deferred as關(guān)鍵字來(lái)指定舱卡,如下例子所示:
import 'package:deferred/hello.dart' deferred as hello;
當(dāng)需要使用的時(shí)候辅肾,需要使用loadLibrary()方法來(lái)加載:
greet() async {
await hello.loadLibrary();
hello.printGreeting();
}
*/