17. Dart中的库 自定义库 系统库和第三方库
Dart 中的库有哪些?
前面介绍 Dart 基础知识的时候基本上都是在一个文件里面编写 Dart 代码的,但实际开发中不可能这么写,模块化很重要,所以这就需要使用到库的概念。
在 Dart 中,库的使用时通过 import 关键字引入的。
library 指令可以创建一个库,每个 Dart 文件都是一个库,即使没有使用 library 指令来指定。
Dart 中的库主要有三种:
- 1、我们自定义的库
import 'lib/xxx.dart';
- 2、系统内置库
import 'dart:math'; import 'dart:io'; import 'dart:convert';
- 3、Pub 包管理系统中的库
pub dev/packages
pub flutter-io/packages
pub dartlang/flutter
如何使用?
1、需要在自己想项目根目录新建一个pubspec.yaml
2、在pubspec.yaml
文件 然后配置名称 、描述、依赖等信息
3、然后运行 pub get
获取包下载到本地
4、项目中引入库 import 'package:http/http.dart' as http;
看文档使用
Dart 中导入自己本地库
这个在之前的学习中我们就使用过,看下面的代码:
import 'lib/Animal.dart'; main(){ var a=new Animal('金毛', 20); print(a.getName()); }
代码中的import 'lib/Animal.dart';
就是自己本地建的库,以后公共的 class 方法相关代码都在这个文件中写,方便维护。
导入系统内置的 math 库
// import 'dart:io'; import "dart:math";//系统内置库 main(){ print(min(12,23)); print(max(12,25)); }
这个不需要大家记,用到那个查那个就行。
导入系统内置库实现请求数据 httpClient
import 'dart:io'; import 'dart:convert'; void main() async{ var result = await getDataFromZhihuAPI(); print(result); } //api 接口: http://news-at.zhihu.com/api/3/stories/latest getDataFromZhihuAPI() async{ //1、创建 HttpClient 对象 var httpClient = new HttpClient(); //2、创建 Uri 对象 var uri = new Uri.http('news-at.zhihu.com','/api/3/stories/latest'); //3、发起请求,等待请求 var request = await httpClient.getUrl(uri); //4、关闭请求,等待响应 var response = await request.close(); //5、解码响应的内容 return await response.transform(utf8.decoder).join(); }
运行结果:
async 和 await
这两个关键字的使用只需要记住两点:
- 只有
async
方法才能使用await
关键字调用方法 - 如果调用别的
async
方法必须使用await
关键字
async
是让方法变成异步。
await
是等待异步方法执行完成。
void main() async { var result = await testAsync(); print(result); } //异步方法 testAsync() async { return 'Hello async'; }
运行结果:
Hello async
Dart 导入 Pub 包管理系统中的库
pub 包管理系统:
1、从下面网址找到要用的库
2、创建一个 pubspec.yaml 文件,内容如下
name: xxx description: A new flutter module project. dependencies: http: ^0.13.0 date_format: ^1.0.6
3、配置dependencies
4、运行pub get
获取远程库
5、看文档引入库使用
import 'dart:convert' as convert; import 'package:http/http.dart' as http; void main(List<String> arguments) async { var url = Uri.https('www.mybj123.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}.'); } }
Dart 库的重命名及 Dart 冲突解决
冲突解决
当引入两个库中有相同名称标识符的时候,如果是 java 通常我们通过写上完整的包名路径来指定使用的具体标识符,甚至不用import
都可以,但是 Dart 里面是必须import
的。当冲突的时候,可以使用as
关键字来指定库的前缀。如下例子所示:
import 'lib/Person1.dart'; import 'lib/Person2.dart' as lib; main(List args) { Person p1 = new Person('mybj', 20); p1.printInfo(); lib.Person p2 = new lib.Person('码云笔记', 3); p2.printInfo(); }
运行结果:
Person1:mybj----20 Person2:码云笔记----3
部分导入
如果只需要导入库的一部分,有两种模式:
模式一:只导入需要的部分,使用show
关键字,如下例子所示:
import'package:lib1/lib1.dart'show foo;
模式二:隐藏不需要的部分,使用hide
关键字,如下例子所示:
import'package:lib2/lib2.dart'hide foo;
实例
// import 'lib/myMath.dart' show getAge; import 'lib/myMath.dart' hide getName; void main(){ //getName(); getAge(); }
延迟加载
也称为懒加载,可以在需要的时候再进行加载。
懒加载的最大好处是可以减少 APP 的启动时间。
懒加载使用deferred as
关键字来指定,如下例子所示:
import 'package:deferred/hello.dart' deferred as hello;
当需要使用的时候,需要使用loadLibrary()
方法来加载:
greet() async { await hello.loadLibrary(); hello.printGreeting(); }
码云笔记 » 17. Dart中的库 自定义库 系统库和第三方库