【发布时间】:2021-11-11 05:18:24
【问题描述】:
I Take a variable in my State File? image;
然后从我的设备访问图像
void filePicker() async {
final File? selectedImage =await ImagePicker.pickImage(source: ImageSource.gallery);
print(selectedImage!.path);
setState(() {
image = selectedImage;
});
}
然后尝试传递 image file 以及其他 http 正文参数。如果我没有传递图像文件,那么 API 没有显示任何错误。但我需要传递图像文件以获得正确的结果。
因为我明确抛出异常,所以它抛出的异常像 Faild to fetch 和 ScaffoldMessenger 中的消息 --> Somthing went wrong
Future<void> SaveCustomTestBooking() async {
var jsonResponse;
if (EncUserId.isNotEmpty) {
var postUri = Uri.parse("http://medbo.digitalicon.in/api/medboapi/SaveCustomTestBooking");
var request = http.MultipartRequest('POST', postUri);
request.fields['VisitDate'] = _selectedDate;
request.fields['EncUserId'] = EncUserId;
request.files.add(new http.MultipartFile.fromBytes('image',await File.fromUri(Uri.parse(image!.path)).readAsBytes(),contentType: new MediaType('image', 'jpeg')));
request.send().then((response) {
if (response.statusCode == 200) {
print("Uploaded!");
Navigator.push(context,MaterialPageRoute(builder: (context) => DieticianAfterDateSelectPage(rresponse:DieticianEncBookingIdModel.fromJson(jsonResponse),)));
} else {
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text("Somthing went wrong")));
throw Exception("Faild to fetch");
}
});
}
}
【问题讨论】:
-
你打印出
base64Encode(image!.readAsBytesSync())看是否已经包含'data:image/jpg;base64,' -
没有。但相反,我在我的设备中显示图像。及其显示图像
-
我也尝试
'UserFile':image.toString(),同样的错误...需要将图像转换为base64吗?