dio 数据请求
注意:Flutter 官方提供了 HttpClient 发起的请求,但 HttpClient 本身功能较弱,很多常用功能都不支持。
所以,官方建议使用 dio 来发起网络请求,它是一个强大易用的 dart http 请求库,支持 Restful API、FormData、拦截器、请求取消、Cookie管理、文件上传/下载……详情请查看 github dio .
-
添加依赖
dependencies:
dio: ^2.0.1 // 当前最新版本为 2.0.1 ,建议使用最新版本(dio:后面要留有空格,不然安装不上)
按Ctrl/Command+S键保存会自动安装 - 使用方法
- get 请求
import 'package:dio/dio.dart';
Dio dio = new Dio();
var response = await dio.get("/test",data:{"id":12,"name":"wendu"})
print(response.data);
-
post 请求
response = await dio.post("/test",data:{“id”:12,“name”:“wendu”})
注意:await 关键字,必须用在被 async 修饰的方法内!例如:void getMovieList() async { }