Johu

10-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包管理系统中的库
    1. 需要在自己想项目根目录新建一个pubspec.yaml
    2. 在pubspec.yaml文件 然后配置名称 、描述、依赖等信息
    3. 然后运行 dart pub get 获取包下载到本地
    4. 项目中引入库 import 'package:http/http.dart' as http; 看文档使用

导入自己的库

// Animal.dart
class Animal {
  String _name; //私有属性
  int age;
  //默认构造函数的简写
  Animal(this._name, this.age);
  String getName() {
    return this._name;
  }
}
// 主文件
import 'lib/Animal.dart';
main(){
  var a=new Animal('小黑狗', 20);
  print(a.getName()); // 小黑狗
}

导入系统内置库

系统内置数学方法库

import "dart:math";

main() {
  print(min(12, 23)); // 12
  print(max(12, 25)); // 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{
  //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(); // {"date":"20211217","stories":...
}

async 和 await

  • 只有async方法才能使用await关键字调用方法
  • 如果调用别的async方法必须使用await关键字

async是让方法变成异步。
await是等待异步方法执行完成。

//异步方法
testAsync() async{
  return 'Hello async';
}

void main() async{
  var result = await testAsync();
  print(result); // Hello async
}

包管理系统的库

import 'package:date_format/date_format.dart';

main() async {
  print(formatDate(DateTime(1989, 2, 21), [yyyy, '*', mm, '*', dd])); // 1989*2*21
}

包的操作

库的重命名

  • 当库冲突的时候,可以使用as关键字来指定库的前缀。
// Person1.dart
class Person {
  String name;
  int age;
  Person(this.name, this.age);

  void printInfo() {
    print("Person1:${this.name}----${this.age}");
  }
}

// Person2.dart
class Person {
  String name;
  int age;
  Person(this.name, this.age);

  void printInfo() {
    print("Person2:${this.name}----${this.age}");
  }
}

// 主文件
import 'lib/Person1.dart';
import 'lib/Person2.dart' as lib;

main(List<String> args) {
  Person p1 = new Person('张三', 20);
  p1.printInfo(); // Person1:张三----20

  lib.Person p2 = new lib.Person('李四', 20);
  p2.printInfo(); // Person2:李四----20
}

部分导入

  • 模式一:只导入需要的部分,使用show关键字
    • import 'package:lib1/lib1.dart' show foo;
  • 模式二:隐藏不需要的部分,使用hide关键字
    • import 'package:lib2/lib2.dart' hide foo;
// myMath.dart
void getName(){
  print('张三');
}
void getAge(){
  print(20);
}
void getSex(){
  print('男');
}

// 主文件
import 'lib/myMath.dart' hide getName;

void main(){
//  getName(); // 找不到这个方法
  getAge(); // 20
}

延迟加载

  • 也称为懒加载,可以在需要的时候再进行加载。
  • 懒加载的最大好处是可以减少APP的启动时间。
  • 懒加载使用deferred as关键字来指定。
  • 需要使用loadLibrary()方法来加载
greet() async {
  await hello.loadLibrary();
  hello.printGreeting();
}
posted @ 2021-12-18 00:14  Johu  阅读(40)  评论(0编辑  收藏  举报