Flutter之dio 数据请求

dio 数据请求

注意:Flutter 官方提供了 HttpClient 发起的请求,但 HttpClient 本身功能较弱,很多常用功能都不支持。

所以,官方建议使用 dio 来发起网络请求,它是一个强大易用的 dart http 请求库,支持 Restful API、FormData、拦截器、请求取消、Cookie管理、文件上传/下载……详情请查看 github dio .

1. 添加依赖

dependencies:
  dio: ^1.0.9  // 当前最新版本为 1.0.9 ,建议使用 pub 上的最新版本

2. 炒鸡简单的使用方式

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 { }

发布了113 篇原创文章 · 获赞 120 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/xueshao110/article/details/90727068