# 库 lib

本文作者:阳九五 (opens new window)

本站地址:https://blog.56321654.xyz (opens new window)

# 导入核心库

import 'dart:io';

void main() {
  var f = new File('README.md');
  var content = f.readAsStringSync();
  print(content);
}

# Dart 语言学习示例
1
2
3
4
5
6
7
8
9

# 导入第三方库

  1. 编写根目录下面的pubspec.yaml文件
dependencies:
  dio: ^4.0.6
1
2
  1. 执行拉取包命令
$ dart pub get
1

3.程序套用

import 'package:dio/dio.dart';

void main() async {
  Dio dio = Dio();
  Response<String> response =
      await dio.get("https://wpapi.ducafecat.tech/products/categories");
  print(response.data);
}

[{"id":34,"name":"Bag","slug":"bag","parent":0,"description":" ......
1
2
3
4
5
6
7
8
9
10

# 导入自己的 git 仓库

  1. 编写根目录下面的pubspec.yaml文件
dependencies:
  uuid:
    git:
      url: https://github.com/Daegalus/dart-uuid
      ref: master
1
2
3
4
5
  1. 执行拉取包命令
$ dart pub get
1

从 master 分支拉取

# 导入类文件

  • phone.dart
class Phone {
  void call() {
    print('Phone is calling...');
  }
}

class Android {
  void playStore() {
    print('Google play store');
  }

  void call() {
    print('Android phone is calling...');
  }
}

class Ios {
  void appleStore() {
    print('Apply store');
  }

  void call() {
    print('Ios phone is calling...');
  }
}
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
  • hello.dart
class Hello {
    void call() {
        print('Hello Dart');
    }
}
1
2
3
4
5

dev_pubspecdev_pubspec.yaml 里面的name字段

import 'package:dev_pubspec/phone.dart';  // phone.dart的路径是在lib文件夹里面
import 'hello.dart';  // hello.dart的路径是相对当前文件

void main() {
    var p = Phone();
    p.call();

    var h = Hello();
    h.call();
}
1
2
3
4
5
6
7
8
9
10

# 前缀&别名

as 关键字可以为package增加一个前缀&别名

import 'package:dart_learn/phone.dart' as pp;

void main() {
  var p = pp.Android();
  p.call();
  p.playStore();
}

Android phone is calling...
Google play store
1
2
3
4
5
6
7
8
9
10

# 筛选包内容

import 'package:dart_learn/phone.dart' show Ios;

void main() {
  var p = Ios();
  p.call();
  p.appleStore();
}

Ios phone is calling...
Apply store
1
2
3
4
5
6
7
8
9
10

hide 筛掉某几个包 show 只使用某几个包

# 延迟载入

deferred 关键字表明这个 package 是延迟加载

import 'package:dart_learn/phone.dart' deferred as pp;

Future<void> main() async {
  await pp.loadLibrary();
  var p = pp.Android();
  p.call();
  p.playStore();
}

Android phone is calling...
Google play store
1
2
3
4
5
6
7
8
9
10
11

loadLibrary() 方式在需要的时候载入包 可提高程序启动速度 用在不常使用的功能 用在载入时间过长的包

最近更新: 8/6/2025, 2:39:35 PM