Dart 库与生态

2023/6/6

# Dart 库与生态

Dart 中的库就是具有特定功能的模块。可能包含单个文件,也可能包含多个文件。

按照库的作者进行划分,库可以分成三类:

  • 自定义库(工程师自己写的)
  • 系统库(Dart 中自带的)
  • 第三方库(Dart 生态中的)

Dart 生态

Dart 与 JavaScript 中的关于库(包)的比较: 20230606162135

# 自定义库

# 通过 library 来声明库

每个 Dart 文件默认都是一个库,只是没有使用 library 来显式声明。

// main.dart
main() {
    print('Hello World');
}

// main.dart
library main; // 默认隐藏了一个 main 的 library 的声明
main() {
    print('Hello World');
}
1
2
3
4
5
6
7
8
9
10

Dart 使用 _ (下划线) 开头的标识符,表示库内访问可见(私有)。

library 关键字声明的库名称建议使用:小写字母+下划线

# 通过 import 来引入库

# 不同类型的库,引入方式不同

  • 自定义库(import '库的位置/库名称.dart'
// MyCustom.dart
library my_custom; // 建议写成小写字母+下划线的形式

class MyCustom {
  String name = 'MyCustom';
  static num version = 1.0;

  void info() {
    print('我是自定义库');
  }
}

// 01.dart
import 'lib/MyCustom.dart';

void main() {
  MyCustom mc = new MyCustom();

  mc.info();
  print(MyCustom.version);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  • 系统库 (import 'dart:库名称')
import 'dart:math';
import 'dart:core'; // core 库会被默认引入

void main() {
  print(pi);

  print(min(3, 6));
  print(max(3, 6));
}
1
2
3
4
5
6
7
8
9
  • 第三方库

# 引入部分库(仅引入需要的内容)

  • 包含引入(show)
  • 排除引入(hide)
// common.dart
void f1() {
  print('f1 is running');
}

void f2() {
  print('f2 is running');
}

void f3() {
  print('f3 is running');
}

// 03.dart
// show 后面指定包含引入的内容
import './lib/common.dart' show f1, f3;

void main() {
  f1();

  // f2();

  f3();
}

// 04.dart
// hide 会隐藏后面的内容
import './lib/common.dart' hide f1, f3;

void main() {
  // f1();

  f2();

  // f3();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

# 指定库前缀

当库名冲突时,可以通过 as 关键字,给库声明一个前缀(或者说给库起个别名)。

// common.dart
void f1() {
  print('f1 is running');
}

void f2() {
  print('f2 is running');
}

void f3() {
  print('f3 is running');
}

// function.dart
void f1() {
  print('f1 of function is running');
}

void hello() {
  print('hello world');
}

// 05.dart
import './lib/common.dart';
import './lib/function.dart' as func; // 给库添加前缀,解决命名冲突的问题

void main() {
  f1();

  func.f1();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

# 延迟引入(懒加载)

使用 deferred as 关键字 来标识需要延时加载的库。

import './lib/function.dart' deferred as func;

void main() {
  // func.hello(); // Deferred library func was not loaded.

  greet();
}

Future greet() async {
  await func.loadLibrary(); // 异步加载库
  func.hello();
}
1
2
3
4
5
6
7
8
9
10
11
12

# 通过 part 与 part of 来组装库

20230608101946

// lib/phone/main.dart
library phone;

import 'dart:math';

// 与分库建立联系
part 'Camera.dart';
part 'Processor.dart';


void main() {
  Camera c = new Camera();
  c.info();

  Processor p = new Processor();
  p.info();

  print(pi);
}

// Camera.dart
// 与主库建立联系
part of phone;

class Camera {
  String name = '摄像头';

  void info() {
    print('我是摄像头');
  }
}

// Processor.dart
// 与主库建立联系
part of phone;

class Processor {
  String name = '处理器';

  void info() {
    print('我是处理器');
  }
}

// 07.dart
import './lib/phone/main.dart' as phone; // main 函数与当前库冲突,所以需要加 phone 前缀

void main() {
  phone.main();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

# 系统库

系统库(也叫核心库)是 Dart 提供的常用内置库。不需要单独下载,就可以直接使用。

引入方式:import 'dart:库名。core 库会自动引入,无需手动引入。

系统库列表:https://dart.cn/guides/libraries (opens new window)

系统库中的时间处理库使用示例:

// import 'dart:core'; // 自动引入

void main() {
  // 创建当前时间
  var now = new DateTime.now();
  print(now);

  // 通过普通构造函数创建时间
  var d = new DateTime(2021, 1, 20, 9, 30);
  print(d);

  // 创建标准时间
  var d1 = DateTime.parse('2021-01-20 12:30:30');
  print(d1);
  var d2 = DateTime.parse('2021-01-20 12:30:30+0800');
  print(d2);

  // 时间增量
  print(now.add(new Duration(hours: 2)));

  // 时间比较
  print(d1.isAfter(d2));  // d1 是否在 d2 之后
  print(d1.isBefore(d2)); // d1 是否在 d2 之前
  print(d1.isAtSameMomentAs(d2)); // d1 与 d2 是否相同

  // 时间差
  var d3 = new DateTime(2021, 1, 1);
  var d4 = new DateTime(2021, 1, 4);
  var difference = d3.difference(d4);
  print([difference.inDays, difference.inHours]); // d1 与 d2 相差的天数与小时

  // 时间戳
  print(now.millisecondsSinceEpoch); // 单位毫秒,13 位时间戳

  // 格式化
  print(now.month.toString().padLeft(2, '0'));
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

# 第三方库

来源:

使用:

  • 在项目目录下创建 pubspec.yaml
  • pubspec.yaml 中声明第三方库(依赖)
  • 命令行中进入 pubspec.yaml 所在目录,执行 pub get 进行安装
  • 项目中引入已安装的第三方库(import 'package:xxxx/xxxx.dart'

第三方库的结构