flutter how to use http package

官方介绍及文档
使用有问题

正确使用方法

import 'package:http/http.dart' as http;

main(List<String> arguments) async {
  var url = "https://api.themoviedb.org/3/discover/movie";

  var body = {
    "api_key": "09564a04a5ab6977006247589da33d46",
    "language": "en-US",
    "sort_by": "popularity.desc",
    "include_adult": "false",
    "include_video": "false",
    "page": "1",
  };

//  方法一
//  var response = await http.post(url,body: body);
//  print(response.body);
//  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}.");
//  }

  //  方法二
  var client = new http.Client();
  try {
    var uriResponse = await client.post(url, body: body);
    print(uriResponse.body);
  } finally {
    client.close();
  }
}
    原文作者:落叶归根
    原文地址: https://segmentfault.com/a/1190000019146820
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞