學(xué)習(xí)筆記,旨在于快速入門和學(xué)習(xí)Dart,其中可能會(huì)有理解錯(cuò)誤,請(qǐng)指出,一起學(xué)習(xí)。
系列文章
2.1、Dart語(yǔ)言基礎(chǔ):變量、運(yùn)算符
2.2、Dart語(yǔ)言基礎(chǔ):函數(shù)與閉包
2.3、Dart語(yǔ)言基礎(chǔ):面向?qū)ο?/a>
2.4、Dart語(yǔ)言基礎(chǔ):異步
2.5、Dart語(yǔ)言基礎(chǔ):庫(kù)與包
...
一、庫(kù)基礎(chǔ)
關(guān)鍵字
library和import:
The import and library directives can help you create a modular and shareable code base.標(biāo)記為內(nèi)部私有:下劃線
underscore (_)庫(kù)可以被打包,然后發(fā)布。
packages
1、庫(kù)的引用:package: scheme
// Importing core libraries。
import 'dart:math';
// Importing libraries from external packages
import 'package:test/test.dart';
// Importing files
import 'path/to/my_other_file.dart';
2、引入的時(shí)候,創(chuàng)建庫(kù)的prefix,關(guān)鍵字as
import 'package:lib1/lib1.dart';
import 'package:lib2/lib2.dart' as lib2;
// Uses Element from lib1.
Element element1 = Element();
// Uses Element from lib2.
lib2.Element element2 = lib2.Element();
3、限制性導(dǎo)入(即導(dǎo)入庫(kù)中的一部分),關(guān)鍵字show/hide
// Import only foo.
import 'package:lib1/lib1.dart' show foo;
// Import all names EXCEPT foo.
import 'package:lib2/lib2.dart' hide foo;
4、延遲加載(Lazily loading a library),一般用于web端,關(guān)鍵字deferred as
import 'package:greetings/hello.dart' deferred as hello;
// 可以異步使用
Future<void> greet() async {
await hello.loadLibrary();
hello.printGreeting();
}
異步、多次使用庫(kù)方法,庫(kù)文件只會(huì)被加載一次。
Here are some cases when you might use deferred loading:
1、To reduce a web app’s initial startup time.
2、To perform A/B testing—trying out alternative implementations of an algorithm, for example.
3、To load rarely used functionality, such as optional screens and dialogs.Keep in mind the following when you use deferred loading:
1、A deferred library’s constants aren’t constants in the importing file. Remember, these constants don’t exist until the deferred library is loaded.
2、You can’t use types from a deferred library in the importing file. Instead, consider moving interface types to a library imported by both the deferred library and the importing file.
3、Dart implicitly inserts loadLibrary() into the namespace that you define using deferred as namespace. The loadLibrary() function returns a Future.
二、庫(kù)的實(shí)現(xiàn)(Implementing libraries)
1、庫(kù)必要結(jié)構(gòu)

- 配置文件:
pubspec.yaml - 庫(kù)目錄:
lib
在lib目錄下的文件,默認(rèn)是對(duì)外公開的。
實(shí)踐中,一般將源碼放在lib/src,src下文件私有;對(duì)外文件,需要使用export額外導(dǎo)出。
例子:shelf的包結(jié)構(gòu)

其中,對(duì)外的文件有shelf_io.dart 和 shelf.dart 兩個(gè)文件。
export 'src/cascade.dart' show Cascade;
...
export 'src/middleware/add_chunked_encoding.dart' show addChunkedEncoding;
...
2、條件導(dǎo)入和導(dǎo)出,Conditionally importing and exporting library files
// lib/hw_mp.dart
export 'src/hw_none.dart' // Stub implementation
if (dart.library.io) 'src/hw_io.dart' // dart:io implementation
if (dart.library.html) 'src/hw_html.dart'; // dart:html implementation
上面代碼的意思:
1、當(dāng)import 'dart:io';時(shí),導(dǎo)出文件src/hw_io.dart。
2、當(dāng)import 'dart:html';時(shí),導(dǎo)出文件src/hw_html.dart。
3、其他,導(dǎo)出文件src/hw_none.dart。
3、包的文檔化
使用工具dartdoc,前提是代碼使用 '///' 進(jìn)行注釋。
三、包,packages
包管理遵循
pub package manager。包存儲(chǔ):
公網(wǎng) pub.dev site、本地、私有服務(wù)器。包管理工具
1、命令行工具dart pub
2、IDE支持插件
包的創(chuàng)建
- 創(chuàng)建配置文件
pubspec.yaml,文件格式
包的獲取,Getting packages
獲取包命令:
dart pub get
dart pub get包引用配置
package_config.json:支持配置包的不同的url地址
從包里面引用庫(kù),Importing libraries from packages
import 'package:js/js.dart' as js;
import 'package:intl/intl.dart';
-
transmogrify包
transmogrify/
lib/
transmogrify.dart
parser.dart
test/
parser/
parser_test.dart
import 'package:transmogrify/parser.dart';
更新包依賴,Upgrading a dependency
- 命令:
dart pub upgrade或dart pub upgrade transmogrify
dart pub upgrade