摘自:https://www.cnblogs.com/loaderman/p/11027076.html
前面介紹Dart基礎(chǔ)知識的時候基本上都是在一個文件里面編寫Dart代碼的,但實際開發(fā)中不可能這么寫,模塊化很重要,所以這就需要使用到庫的概念。
在Dart中,庫的使用時通過import關(guān)鍵字引入的。
library指令可以創(chuàng)建一個庫,每個Dart文件都是一個庫,即使沒有使用library指令來指定。
Dart中的庫主要有三種:
1、我們自定義的庫
import 'lib/xxx.dart';
2、系統(tǒng)內(nèi)置庫
import 'dart:math';
import 'dart:io';
import 'dart:convert';
3、Pub包管理系統(tǒng)中的庫
https://pub.dev/packages
https://pub.flutter-io.cn/packages
https://pub.dartlang.org/flutter/
1、需要在自己想項目根目錄新建一個pubspec.yaml
2、在pubspec.yaml文件 然后配置名稱 、描述、依賴等信息

image.png
3、然后運行 pub get 獲取包下載到本地
4、項目中引入庫 import 'package:http/http.dart' as http; 看文檔使用
導(dǎo)入本地庫
import 'lib/Animal.dart';
main(){
var a=new Animal('小黑狗', 20);
print(a.getName());
}
class Animal{
String _name; //私有屬性
int age;
//默認(rèn)構(gòu)造函數(shù)的簡寫
Animal(this._name,this.age);
void printInfo(){
print("${this._name}----${this.age}");
}
String getName(){
return this._name;
}
void _run(){
print('這是一個私有方法');
}
execRun(){
this._run(); //類里面方法的相互調(diào)用
}
}
導(dǎo)入系統(tǒng)內(nèi)置的庫
// 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對象
var httpClient = new HttpClient();
//2、創(chuàng)建Uri對象
var uri = new Uri.http('news-at.zhihu.com','/api/3/stories/latest');
//3、發(fā)起請求,等待請求
var request = await httpClient.getUrl(uri);
//4、關(guān)閉請求,等待響應(yīng)
var response = await request.close();
//5、解碼響應(yīng)的內(nèi)容
return await response.transform(utf8.decoder).join();
}
說明
/*
async和await
這兩個關(guā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)入第三方庫
/*
pub包管理系統(tǒng):
1、從下面網(wǎng)址找到要用的庫
https://pub.dev/packages
https://pub.flutter-io.cn/packages
https://pub.dartlang.org/flutter/
2、創(chuàng)建一個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、運行pub get 獲取遠(yuǎn)程庫
5、看文檔引入庫使用
*/
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庫的重命名 Dart沖突解決
/*
1、沖突解決
當(dāng)引入兩個庫中有相同名稱標(biāo)識符的時候,如果是java通常我們通過寫上完整的包名路徑來指定使用的具體標(biāo)識符,甚至不用import都可以,但是Dart里面是必須import的。當(dāng)沖突的時候,可以使用as關(guān)鍵字來指定庫的前綴。如下例子所示:
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)入庫的一部分,有兩種模式:
模式一:只導(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();
}
延遲加載
/*
延遲加載
也稱為懶加載,可以在需要的時候再進(jìn)行加載。
懶加載的最大好處是可以減少APP的啟動時間。
懶加載使用deferred as關(guān)鍵字來指定,如下例子所示:
import 'package:deferred/hello.dart' deferred as hello;
當(dāng)需要使用的時候,需要使用loadLibrary()方法來加載:
greet() async {
await hello.loadLibrary();
hello.printGreeting();
}
*/