所谓的库,就是一些功能的集合,通过封装,我们可以在项目中通过引用来使用。
库在Dart中分为几种
自定义的库
- 创建
把类、接口、方法等内容单独抽离当如Dart文件
- 使用
import './xxx'
./person.dart
class Person {String name;int age;Person(this.name, this.age);showName() {return this.name;}showAge() {return this.age;}}show() {return 1;}
./1.dart
import "./Person.dart";void main() {var p1 = Person('张三', 18);print(p1.name); //张三print(p1.showAge()); //18show(); //1}
系统库
系统定义的库,这里面有两种:
(1)一种核心库:内置库(该库会被自动导入每个Dart程序)包含:
- 控制台打印:
print
- 数字
.parse 、.toString()
- 字符串正则
.contains 、.statsWith 、.endsWith 、…
- 集合List、Set、map
.add 、.addAll 、.sort
- URI
编码和解码完整合法的URI encodeFull 、decodeFull()
编码和解码 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"
import "dart:math";// import "dart:async"; //从Dart 2.1 开始不用引入void main() {// 数学方法print(max(10, 20)); //20print(min(10, 20)); //10// 异步asyncshow2() {return 2;}show() async {var s1 = await show2();print(s1);}show(); //2}
main函数也可以写成异步
import "dart:math";show2() {return 2;}show() async {var s1 = await show2();print(s1);}void main() async {var a = await show(); //2}
第三方的库
第三方库 pub包中的库
第三方地址:
https://pub.dev/packageshttps://pub.flutter-io.cn/packageshttps://pub.dartlang.org/flutter/
1、需要在项目目录下创建 pubspec.yaml 文件
2、在pubspec.yaml文件中配置名称、描述、依赖等信息
3、运行 pub get 获取包下载到本地
4、项目中引用库 import "package://http://xxx"
导入库时,可以使用as关键字来给库起别名,避免命名空间冲突。
import 'package:lib1/lib1.dart';import 'package:lib2/lib2.dart' as lib2;// 使用lib1中的ElementElement element1 = new Element();// 使用lib2中的Elementlib2.Element element2 = new lib2.Element();
//Person.dartclass Person {}show() {return 1;}//1.dartimport "./Person.dart";import "./Person.dart" as aa;void main() {print(aa.show()); //1}
使用show和hide关键字控制库中成员的可见性
// 仅导入foo,屏蔽库中其他成员import 'package:lib1/lib1.dart' show foo;// 屏蔽foo,库中其他成员都可见import 'package:lib2/lib2.dart' hide foo;
为了减少 APP 的启动时间,加载很少使用的功能,我们还可以延迟导入库。使用 deferred as关键字延迟导入
import 'package:deferred/hello.dart' deferred as hello;// 当需要使用时,再通过库标识符调用 loadLibrary函数加载hello.loadLibrary();
创建pubspec.yaml
name: demodescription: playdependencies:http: ^0.12.2
运行命令 pub get 或者 flutter pub get
引用http库示例:
import 'dart:convert' as convert;import 'package:http/http.dart' as http;void main(List<String> arguments) async {// This example uses the Google Books API to search for books about http.// https://developers.google.com/books/docs/overviewvar url = 'https://www.googleapis.com/books/v1/volumes?q={http}';// Await the http get response, then decode the json-formatted response.var response = await http.get(url);if (response.statusCode == 200) {var jsonResponse = convert.jsonDecode(response.body);var itemCount = jsonResponse['totalItems'];print('Number of books about http: $itemCount.');} else {print('Request failed with status: ${response.statusCode}.');}}
引入 本地库

使用
dependencies:flutter:sdk: flutter# The following adds the Cupertino Icons font to your application.# Use with the CupertinoIcons class for iOS style icons.cupertino_icons: ^0.1.2#本地插件引用 注意缩进格式utils:path: plugins/utils
import 'package:flutter/material.dart';import "package:utils/Base.dart";//... color: Color(Base.getRandomColor()),
引入 本地项目文件(绝对路径方式)

./pubspec.yaml
name: myApp
./lib/MyHomePage.dart
import "package:myApp/utils/Base.dart"; //以绝对路径引入本地项目里文件//... 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:
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.
