推荐使用dio
官方用的是http,但是好多坑啊…,而且能力较弱
所以 使用dio吧,以下是官方文档
https://pub.dartlang.org/packages/dio
简易教程
https://www.cnblogs.com/fuyaozhishang/p/9252286.html
添加依赖
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2
http: ^0.12.0+1
dio: ^1.0.13
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”)]);
下载文件:
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)>
我的例子
import 'dart:convert';
import 'dart:io';
import 'dart:async';
import 'package:dio/dio.dart';
void main(){
// base64('187233');
postFind('dart');
// getFind('book');
}
base64(String str1) {
var bytes = utf8.encode(str1);
var encoded1 = base64Encode(bytes);
// print(encoded1); // 返回编码列表
// print(base64Decode(encoded1)); // 返回编码ac列表
// print(String.fromCharCodes(base64Decode(encoded1))); // 返回解码
return encoded1;
}
postFind(book) async{
Dio dio = new Dio();
FormData formData = new FormData.from({"username": book});
var response = await dio.post("http://139.199.169.159:90/signin", data: formData);
print(response);
}
getFind(book) async{
Dio dio = new Dio();
var response = await dio.get("http://139.199.169.159:90/$book");
print(response);
}
渲染
https://blog.csdn.net/sinat_17775997/article/details/82107779
https://blog.csdn.net/starshus/article/details/81570043
https://flutterchina.club/cookbook/networking/fetch-data/