我一直在使用flutter(https://pub.dartlang.org/packages/multi_image_picker)的Multi Image Picker库。最近,我在图像上传方面遇到问题。 问题是,当我检查日志时,图像被多次上传。 我想一次上传特定的图片。
void _loadImage() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
Uri uri = Uri.parse(SAMPLE URL);
var request = http.MultipartRequest("POST", uri);
request.headers['authorization'] = getAccessToken(prefs);
ByteData byteData = await this._asset.requestOriginal();
List<int> imageData = byteData.buffer.asUint8List();
var multipartfile = http.MultipartFile.fromBytes(
'image',
imageData,
filename: 'image.jpg',
contentType: MediaType("image", "jpg"),
);
request.files.add(multipartfile);
var response = await request.send();
String responseData = await response.stream
.transform(utf8.decoder)
.join(); // decodes on response data using UTF8.decoder
//print(responseData);
Map data = (json.decode(responseData)); // Parse data from JSON string
if (response.statusCode == 200) {
await finalData.add(data);
}
print(finalData);
this._asset.releaseOriginal();
print(response.statusCode);
if (response.statusCode != 200) {
print("Error Uploading");
} else {
await this._asset.requestThumbnail(300, 300, quality: 50);
}
if (this.mounted) {
setState(() {});
}
}