所谓的库,就是一些功能的集合,通过封装,我们可以在项目中通过引用来使用。

库在Dart中分为几种

自定义的库

  • 创建

把类、接口、方法等内容单独抽离当如Dart文件

  • 使用

import './xxx'

./person.dart

  1. class Person {
  2. String name;
  3. int age;
  4. Person(this.name, this.age);
  5. showName() {
  6. return this.name;
  7. }
  8. showAge() {
  9. return this.age;
  10. }
  11. }
  12. show() {
  13. return 1;
  14. }

./1.dart

  1. import "./Person.dart";
  2. void main() {
  3. var p1 = Person('张三', 18);
  4. print(p1.name); //张三
  5. print(p1.showAge()); //18
  6. show(); //1
  7. }

系统库

系统定义的库,这里面有两种:
(1)一种核心库:内置库(该库会被自动导入每个Dart程序)包含:

  • 控制台打印:

print

  • 数字

.parse.toString()

  • 字符串正则

.contains.statsWith.endsWith 、…

  • 集合List、Set、map

.add.addAll.sort

  • URI

编码和解码完整合法的URI encodeFulldecodeFull()
编码和解码 URI 组件 encodeComponent()decodeComponent()
URI 解析构建 parse()Uri()

  • DateTime

获取当前时间 DateTime.now()
创建DateTime对象 DateTime(2020, 10, 10)
返回现在距离1970.1.1日的毫秒数 DateTime().millisecondsSinceEpoch

(2)其它系统库
这个是需要手动导入的。
dart:async - 异步编程 从dart2.1开始不用引入(async、await)
dart:math

引入 import "dart:math"

  1. import "dart:math";
  2. // import "dart:async"; //从Dart 2.1 开始不用引入
  3. void main() {
  4. // 数学方法
  5. print(max(10, 20)); //20
  6. print(min(10, 20)); //10
  7. // 异步async
  8. show2() {
  9. return 2;
  10. }
  11. show() async {
  12. var s1 = await show2();
  13. print(s1);
  14. }
  15. show(); //2
  16. }

main函数也可以写成异步

  1. import "dart:math";
  2. show2() {
  3. return 2;
  4. }
  5. show() async {
  6. var s1 = await show2();
  7. print(s1);
  8. }
  9. void main() async {
  10. var a = await show(); //2
  11. }

第三方的库

第三方库 pub包中的库
第三方地址:

  1. https://pub.dev/packages
  2. https://pub.flutter-io.cn/packages
  3. https://pub.dartlang.org/flutter/

1、需要在项目目录下创建 pubspec.yaml 文件
2、在pubspec.yaml文件中配置名称、描述、依赖等信息
3、运行 pub get 获取包下载到本地
4、项目中引用库 import "package://http://xxx"

导入库时,可以使用as关键字来给库起别名,避免命名空间冲突。
  1. import 'package:lib1/lib1.dart';
  2. import 'package:lib2/lib2.dart' as lib2;
  3. // 使用lib1中的Element
  4. Element element1 = new Element();
  5. // 使用lib2中的Element
  6. lib2.Element element2 = new lib2.Element();
  1. //Person.dart
  2. class Person {}
  3. show() {
  4. return 1;
  5. }
  6. //1.dart
  7. import "./Person.dart";
  8. import "./Person.dart" as aa;
  9. void main() {
  10. print(aa.show()); //1
  11. }

使用showhide关键字控制库中成员的可见性
  1. // 仅导入foo,屏蔽库中其他成员
  2. import 'package:lib1/lib1.dart' show foo;
  3. // 屏蔽foo,库中其他成员都可见
  4. import 'package:lib2/lib2.dart' hide foo;

为了减少 APP 的启动时间,加载很少使用的功能,我们还可以延迟导入库。使用 deferred as关键字延迟导入

  1. import 'package:deferred/hello.dart' deferred as hello;
  2. // 当需要使用时,再通过库标识符调用 loadLibrary函数加载
  3. hello.loadLibrary();

创建pubspec.yaml

  1. name: demo
  2. description: play
  3. dependencies:
  4. http: ^0.12.2

运行命令 pub get 或者 flutter pub get

引用http库示例:

  1. import 'dart:convert' as convert;
  2. import 'package:http/http.dart' as http;
  3. void main(List<String> arguments) async {
  4. // This example uses the Google Books API to search for books about http.
  5. // https://developers.google.com/books/docs/overview
  6. var url = 'https://www.googleapis.com/books/v1/volumes?q={http}';
  7. // Await the http get response, then decode the json-formatted response.
  8. var response = await http.get(url);
  9. if (response.statusCode == 200) {
  10. var jsonResponse = convert.jsonDecode(response.body);
  11. var itemCount = jsonResponse['totalItems'];
  12. print('Number of books about http: $itemCount.');
  13. } else {
  14. print('Request failed with status: ${response.statusCode}.');
  15. }
  16. }

引入 本地库

image.png

使用
  1. dependencies:
  2. flutter:
  3. sdk: flutter
  4. # The following adds the Cupertino Icons font to your application.
  5. # Use with the CupertinoIcons class for iOS style icons.
  6. cupertino_icons: ^0.1.2
  7. #本地插件引用 注意缩进格式
  8. utils:
  9. path: plugins/utils
  1. import 'package:flutter/material.dart';
  2. import "package:utils/Base.dart";
  3. //... color: Color(Base.getRandomColor()),

引入 本地项目文件(绝对路径方式)

image.png

./pubspec.yaml

  1. name: myApp

./lib/MyHomePage.dart

  1. import "package:myApp/utils/Base.dart"; //以绝对路径引入本地项目里文件
  2. //... color: Color(Base.getRandomColor()),

环境变量

flutter镜像

flutter 国内镜像配置:
Linux 或 Mac:
export PUB_HOSTED_URL=https://pub.flutter-io.cn
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn

Windows:
新增两个环境变量——用户变量即可
PUB_HOSTED_URL : https://pub.flutter-io.cn
FLUTTER_STORAGE_BASE_URL : https://storage.flutter-io.cn

为 pub 命令配置环境变量

https://dart.cn/tools/pub/environment-variables

Environment variables allow you to customize pub to suit your needs.
PUB_CACHESome of pub’s dependencies are downloaded to the pub cache. By default, this directory is located under .pub-cache in your home directory (on Mac and Linux), or in %LOCALAPPDATA%\Pub\Cache (on Windows). (The precise location of the cache may vary depending on the Windows version.) You can use the PUB_CACHE environment variable to specify another location. For more information, see The system package cache.PUB_HOSTED_URLPub downloads dependencies from the pub.dev site. To specify the location of a particular mirror server, use the PUB_HOSTED_URL environment variable. For example:

  1. PUB_HOSTED_URL = http://user:password@177.0.0.1:9999

Note: If you are attempting to use pub get behind a corporate firewall and it fails, please see pub get fails from behind a corporate firewall for information on how to set up the proxy environment variables for your platform.