移动开发小白修炼手册 Flutter 学习前期--Dart语言学习——day08

异步

 async和await

  这两个关键字的使用只需要记住两点:

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

  async是让方法变成异步。

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

泛型 库

/** 使用 第三方 pub 包管理系统
 * 
 * 1、从下面网址找到要用的库
 * https://pub.dev/packages
 * https://pub.flutter-io.cn/packages
 * https://pub.dartlang.org/flutter/
 * 
 * 
 * 2、创建一Tpubspec.yaml文件,内容如下
 *     name: XXX
 *     description: A new flutter module project.
 *     dependencies:
 *       http: ^0.12.0+2
 *       date_format: ^1.0.6
 * 3、配置dependencies
 * 4、运行put get 获取远程库
 * 5、看文档引入库使用
 */
import 'dart:convert' as convert;
import 'package:http/http.dart' as http;

void main(List<String> arguments) async {
  // This example uses the Google Books API to search for books about http.
  // https://developers.google.com/books/docs/overview
  var url =
      Uri.https('www.googleapis.com', '/books/v1/volumes', {'q': '{http}'});

  // 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}.');
  }
}

脑袋大了 

猜你喜欢

转载自blog.csdn.net/c202003/article/details/114604205