Dart学习记录(四)—— 库
1、引入库 import
2、系统内置库
dart.math
dart.io 请求接口 awaut, async
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(); }
3、引入第三方模块
1、创建一个pubspec.yaml文件,内容如下
name: xxx description: A new flutter module project. dependencies: http: ^0.12.0+2 date_format: ^1.0.6
2、配置dependencies
3、运行pub get 获取远程库
4、重命名引入的库 as
import 'lib/main.dart' as lib;
5、部门引入 show hide
import 'lib/myMath.dart' show getAge;
import 'lib/myMath.dart' hide getName;
6、延迟加载 deferred as
import 'package:deferred/hello.dart' deferred as hello;