/* 前面介绍Dart基础知识的时候基本上都是在一个文件里面编写Dart代码的,但实际开发中不可能这么写,模块化很重要,因此这就须要使用到库的概念。 在Dart中,库的使用时经过import关键字引入的。 library指令能够建立一个库,每一个Dart文件都是一个库,即便没有使用library指令来指定。 Dart中的库主要有三种: 一、咱们自定义的库 import 'lib/xxx.dart'; 二、系统内置库 import 'dart:math'; import 'dart:io'; import 'dart:convert'; 三、Pub包管理系统中的库 https://pub.dev/packages https://pub.flutter-io.cn/packages https://pub.dartlang.org/flutter/ 一、须要在本身想项目根目录新建一个pubspec.yaml 二、在pubspec.yaml文件 而后配置名称 、描述、依赖等信息 三、而后运行 pub get 获取包下载到本地 四、项目中引入库 import 'package:http/http.dart' as http; 看文档使用 */
导入本地库php
import 'lib/Animal.dart'; main(){ var a=new Animal('小黑狗', 20); print(a.getName()); }
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(); //类里面方法的相互调用 } }
导入系统内置的库java
// import 'dart:io'; import "dart:math"; main(){ print(min(12,23)); print(max(12,25)); }
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{ //一、建立HttpClient对象 var httpClient = new HttpClient(); //二、建立Uri对象 var uri = new Uri.http('news-at.zhihu.com','/api/3/stories/latest'); //三、发起请求,等待请求 var request = await httpClient.getUrl(uri); //四、关闭请求,等待响应 var response = await request.close(); //五、解码响应的内容 return await response.transform(utf8.decoder).join(); }
说明:json
/* async和await 这两个关键字的使用只须要记住两点: 只有async方法才能使用await关键字调用方法 若是调用别的async方法必须使用await关键字 async是让方法变成异步。 await是等待异步方法执行完成。 */ void main() async{ var result = await testAsync(); print(result); } //异步方法 testAsync() async{ return 'Hello async'; }
导入第三方库api
/* pub包管理系统: 一、从下面网址找到要用的库 https://pub.dev/packages https://pub.flutter-io.cn/packages https://pub.dartlang.org/flutter/ 二、建立一个pubspec.yaml文件,内容以下 name: xxx description: A new flutter module project. dependencies: http: ^0.12.0+2 date_format: ^1.0.6 三、配置dependencies 四、运行pub get 获取远程库 五、看文档引入库使用 */ import 'dart:convert' as convert; import 'package:http/http.dart' as http; import 'package:date_format/date_format.dart'; main() 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 responce. // var response = await http.get(url); // if (response.statusCode == 200) { // var jsonResponse = convert.jsonDecode(response.body); // print(jsonResponse); // } else { // print("Request failed with status: ${response.statusCode}."); // } print(formatDate(DateTime(1989, 2, 21), [yyyy, '*', mm, '*', dd])); }
Dart库的重命名 Dart冲突解决app
/* 一、冲突解决 当引入两个库中有相同名称标识符的时候,若是是java一般咱们经过写上完整的包名路径来指定使用的具体标识符,甚至不用import均可以,可是Dart里面是必须import的。当冲突的时候,可使用as关键字来指定库的前缀。以下例子所示: 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('李四', 20); p2.printInfo(); }
部分到导入:异步
/* 部分导入 若是只须要导入库的一部分,有两种模式: 模式一:只导入须要的部分,使用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' ; void main(){ getName(); }
延迟加载async
/* 延迟加载 也称为懒加载,能够在须要的时候再进行加载。 懒加载的最大好处是能够减小APP的启动时间。 懒加载使用deferred as关键字来指定,以下例子所示: import 'package:deferred/hello.dart' deferred as hello; 当须要使用的时候,须要使用loadLibrary()方法来加载: greet() async { await hello.loadLibrary(); hello.printGreeting(); } */