Flutter学习笔记1.8 Dart基础(第三方库导入 使用 , Async Await.dart 延迟加载)

281 阅读2分钟

##第三方库导入 在Dart中,库的使用时通过import关键字引入的。 library指令可以创建一个库,每个Dart文件都是一个库,即使没有使用library指令来指定。 Dart中的库主要有三种:

1、我们自定义的库     
      import 'lib/xxx.dart';
2、系统内置库       
      import 'dart:math';    
      import 'dart:io'; 
      import 'dart:convert';
3、Pub包管理系统中的库  
    https://pub.dev/packages
    https://pub.flutter-io.cn/packages
    https://pub.dartlang.org/flutter/

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

###导入第三方库示例 1、从下面网址找到要用的库

https://pub.dev/packages
https://pub.flutter-io.cn/packages
https://pub.dartlang.org/flutter/

2、创建一个pubspec.yaml文件,内容如下

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;
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]));
}

###导入库冲突解决 当引入两个库中有相同名称标识符的时候,如果是java通常我们通过写上完整的包名路径来指定使用的具体标识符,甚至不用import都可以,但是Dart里面是必须import的。当冲突的时候,可以使用as关键字来指定库的前缀。

如下例子所示:

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;   

##async和await 这两个关键字的使用只需要记住两点: 只有async方法才能使用await关键字调用方法 如果调用别的async方法必须使用await关键字 说明: async是让方法变成异步。 await是等待异步方法执行完成。

//异步调用
void main() async{
  var result = await testAsync();
  print(result);

}

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

##延迟加载 也称为懒加载,可以在需要的时候再进行加载。 懒加载的最大好处是可以减少APP的启动时间。

懒加载使用deferred as关键字来指定,如下例子所示:

    import 'package:deferred/hello.dart' deferred as hello;

当需要使用的时候,需要使用loadLibrary()方法来加载:


    greet() async {
      await hello.loadLibrary();
      hello.printGreeting();
    }