flutter how to use http package

官方介紹及文檔
使用有問題json

正確使用方法api

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();
  }
}
相關文章
相關標籤/搜索