参考
try {
Response r = await dio.get("https://www.instagram.com");
print(r.headers);
}on DioError catch(e){
print(e);
print(e.response.statusCode);
}
我的代码操作如下
void _submitData () async {
try {
List<MultipartFile> imageList = new List<MultipartFile>();
for (Asset asset in images) {
ByteData byteData = await asset.getByteData();
List<int> imageData = byteData.buffer.asUint8List();
MultipartFile multipartFile = new MultipartFile.fromBytes(
imageData,
filename: 'load_image',
contentType: MediaType("image", "jpg"),
);
imageList.add(multipartFile);
}
FormData formData = FormData.fromMap({
"photos" : imageList,
"text" : _momentController.text
});
var apiUrl = "http://47.242.63.216:9527/v1/moment";
SharedPreferences prefs = await SharedPreferences.getInstance();
var tokens = prefs.getString("token");
Map<String, dynamic> params = Map();
Response result = await Dio().post(apiUrl,
data: formData, options: Options(headers: {"x-token": tokens}));
debugPrint("${result}");
Map<String, dynamic> authCode = json.decode(result.toString());
var mes = PublishMomentData.fromJson(authCode);
if (mes.code == 200) {
Fluttertoast.showToast(
msg: "发布成功",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
timeInSecForIosWeb: 10,
backgroundColor: Colors.white,
textColor: Colors.black,
fontSize: 16.0);
Navigator.pop(context);
Navigator.pop(context);
} else {
Fluttertoast.showToast(
msg: "发布失败",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
timeInSecForIosWeb: 10,
backgroundColor: Colors.white,
textColor: Colors.black,
fontSize: 16.0);
}
}on DioError catch(e){
Fluttertoast.showToast(
msg: "${e}",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
timeInSecForIosWeb: 10,
backgroundColor: Colors.white,
textColor: Colors.black,
fontSize: 16.0);
print(e);
print(e.response.statusCode);
}
}
|