# 14.库
# 自定义库
lib/Animal.dart
class Animal {
String _name; // 私有属性
int age;
// 默认构造函数的简写
Animal(this._name, this.age);
void printInfo() {
print('${this._name}----${this.age}');
}
String getName() {
return this._name;
}
void _run() {
print('这是一个私有方法');
}
execRun() {
this._run(); // 类里面方法的相互调用
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
main
import 'lib/Animal.dart';
main() {
var a = new Animal('小黑狗',20);
print(a.getName()); // 小黑狗
}
1
2
3
4
5
6
2
3
4
5
6
# 系统库
import 'dart:math';
main() {
print(min(12,23));
print(max(12,25));
}
1
2
3
4
5
6
7
2
3
4
5
6
7
# 第三方库
/**
* pub包管理系统中的库
*
* 1.从下面网址找到要用的库
* https://pub.dev/packages
* https://pub.flutter-io.cn/packages
* https://pub.dartlang.org/flutter/
*
* 2.创建一个pubspec.yaml文件, 内容如下
*
* name: xxx
* description: A new flutter module project
* dependencies:
* http: ^0.12.0+2
* 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 = 'http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20&page=1';
// 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}.');
}
}
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
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
# 库冲突解决
/**
* 1.冲突解决
* 当引入两个库中有相同名称标识符的时候, 如果是java通常我们通过写上完整的包名路径来指定使用
*
* import 'package:lib1/lib1.dart';
* import 'package:lib2/lib2.dart' as lib2
*
* Element element1 = new Element(); // Uses Element from lib1
* lib2.Element element2 = new lib2.Element() // uses Element from lib2
*
*/
import 'lib/Person1.dart';
import 'lib/Person2.dart' as lib;
main(List<String> args) {
Person p1 = new Person('张三',20);
p1.printInfo();
lib.Person p2 = new lib.Person('李四',24);
p2.printInfo();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 部分导入
/**
* 部分导入
* 如果只需要导入库的一部分, 有两种模式:
*
* 模式一: 只导入需要的部分, 使用show关键字, 如下例子所示:
* import 'package:lib1/lib1.dart' show foo;
*
* 模式二: 隐藏不需要的部分, 使用hide关键字, 如下例子所示:
* import 'package:lib2/lib2.dart' hide foo;
*/
// import 'lib/myMath.dart' show getName;
import 'lib/myMath.dart' hide getAge;
void main() {
getName();
// getAge();
getSex();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 延迟加载
/**
* 延迟加载
*
* 也称为懒加载, 可以在需要的时候再进行加载
* 懒加载的最大好处可以减少App的启动时间
*
* 懒加载使用 deferred as关键字来指定, 如下例子所示:
*
* import 'package:deferred/hello.dart' deferred as hello;
*
* 当需要使用的时候, 需要使用loadLibrary()方法来加载;
*
* greet() async {
* await hello.loadLibrary();
* hello.printGreeting();
* }
*/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17