在pubspec.yaml中引入 http 包
dependencies: flutter: sdk: flutter http: ^0.12.1
这里需注意缩进,不然会报错
然后点击右上的 Pub get 下载依赖
下载完成后 引包
import 'package:http/http.dart' as http;
编写HttpUtil
import 'package:http/http.dart' as http;
class NetUtils{
static Future<String> postJson(String url,String json) async{
if(url==null){
throw '地址不能为空';
}
// print(json);
http.Response response = await http.post(
url,// post地址
headers:{"content-type" : "application/json"},//设置content-type为json
body: json//json参数
);
return response.body;
}
}