推荐使用dio

官方用的是http,但是好多坑啊…,而且能力较弱
所以 使用dio吧,以下是官方文档
https://pub.dartlang.org/packages/dio

简易教程

https://www.cnblogs.com/fuyaozhishang/p/9252286.html

添加依赖

  1. dependencies:
  2. flutter:
  3. sdk: flutter
  4. # The following adds the Cupertino Icons font to your application.
  5. # Use with the CupertinoIcons class for iOS style icons.
  6. cupertino_icons: ^0.1.2
  7. http: ^0.12.0+1
  8. dio: ^1.0.13
  9. html: ^0.13.3+3

发起一个 GET 请求 :

Response response; response=await dio.get(“/test?id=12&name=wendu”) print(response.data.toString()); // 请求参数也可以通过对象传递,上面的代码等同于: response=await dio.get(“/test”,data:{“id”:12,”name”:”wendu”}) print(response.data.toString());

发起一个 POST 请求:

response=await dio.post(“/test”,data:{“id”:12,”name”:”wendu”})

发起多个并发请求:

response= await Future.wait([dio.post(“/info”),dio.get(“/token”)]);

下载文件:

  1. response=await dio.download("https://www.google.com/","./xx.html")

发送 FormData:

FormData formData = new FormData.from({ “name”: “wendux”, “age”: 25, }); response = await dio.post(“/info”, data: formData)

通过FormData上传多个文件:

FormData formData = new FormData.from({ “name”: “wendux”, “age”: 25, “file1”: new UploadFileInfo(new File(“./upload.txt”), “upload1.txt”) “file2”: new UploadFileInfo(new File(“./upload.txt”), “upload2.txt”) }); response = await dio.post(“/info”, data: formData)>

我的例子

  1. import 'dart:convert';
  2. import 'dart:io';
  3. import 'dart:async';
  4. import 'package:dio/dio.dart';
  5. void main(){
  6. // base64('187233');
  7. postFind('dart');
  8. // getFind('book');
  9. }
  10. base64(String str1) {
  11. var bytes = utf8.encode(str1);
  12. var encoded1 = base64Encode(bytes);
  13. // print(encoded1); // 返回编码列表
  14. // print(base64Decode(encoded1)); // 返回编码ac列表
  15. // print(String.fromCharCodes(base64Decode(encoded1))); // 返回解码
  16. return encoded1;
  17. }
  18. postFind(book) async{
  19. Dio dio = new Dio();
  20. FormData formData = new FormData.from({"username": book});
  21. var response = await dio.post("http://139.199.169.159:90/signin", data: formData);
  22. print(response);
  23. }
  24. getFind(book) async{
  25. Dio dio = new Dio();
  26. var response = await dio.get("http://139.199.169.159:90/$book");
  27. print(response);
  28. }

渲染

https://blog.csdn.net/sinat_17775997/article/details/82107779
https://blog.csdn.net/starshus/article/details/81570043
https://flutterchina.club/cookbook/networking/fetch-data/