2.5、Dart語言基礎:庫與包

學習筆記,旨在于快速入門和學習Dart,其中可能會有理解錯誤,請指出,一起學習。

系列文章

2.1、Dart語言基礎:變量、運算符
2.2、Dart語言基礎:函數(shù)與閉包
2.3、Dart語言基礎:面向對象
2.4、Dart語言基礎:異步
2.5、Dart語言基礎:庫與包
...

一、庫基礎

  • 關鍵字libraryimport:
    The import and library directives can help you create a modular and shareable code base.

  • 標記為內部私有:下劃線underscore (_)

  • 庫可以被打包,然后發(fā)布。
    packages

1、庫的引用: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、引入的時候,創(chuàng)建庫的prefix,關鍵字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、限制性導入(即導入庫中的一部分),關鍵字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端,關鍵字deferred as

import 'package:greetings/hello.dart' deferred as hello;

// 可以異步使用
Future<void> greet() async {
  await hello.loadLibrary();
  hello.printGreeting();
}
  • 異步、多次使用庫方法,庫文件只會被加載一次。

  • 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.

二、庫的實現(xiàn)(Implementing libraries)

Creating packages

1、庫必要結構

image.png
  • 配置文件:pubspec.yaml
  • 庫目錄:lib
    lib目錄下的文件,默認是對外公開的。
    實踐中,一般將源碼放在lib/src,src下文件私有;對外文件,需要使用export額外導出。
例子:shelf的包結構
image.png

其中,對外的文件有shelf_io.dart 和 shelf.dart 兩個文件。

export 'src/cascade.dart' show Cascade;
...
export 'src/middleware/add_chunked_encoding.dart' show addChunkedEncoding;
...

2、條件導入和導出,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、當import 'dart:io';時,導出文件src/hw_io.dart。
2、當import 'dart:html';時,導出文件src/hw_html.dart。
3、其他,導出文件src/hw_none.dart。

3、包的文檔化

使用工具dartdoc,前提是代碼使用 '///' 進行注釋。

三、包,packages

How to use packages

  • 包管理遵循pub package manager。

  • 包存儲:
    公網(wǎng) pub.dev site、本地、私有服務器。

  • 包管理工具
    1、命令行工具dart pub
    2、IDE支持插件

包的創(chuàng)建

  • 創(chuàng)建配置文件 pubspec.yaml,文件格式

包的獲取,Getting packages

  • 獲取包命令:dart pub get
    dart pub get

  • 包引用配置package_config.json:支持配置包的不同的url地址

從包里面引用庫,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

參考

Developing packages & plugins

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容