我在我的狀態下取了一個變數File? image;
然后從我的設備上訪問影像
void filePicker() async {
final File? selectedImage =await ImagePicker.pickImage(source: ImageSource.gallery)。
print(selectedImage!.path) 。
setState(() {
image = selectedImage。
});
}
然后我把影像檔案和其他的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'/span>, postUri)。
request.field['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(response:DieticianEncBookingIdModel.fromJson(jsonResponse),))。
} else {
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text("Somthing went wrong") )。
throw Exception(" Faild to fetch")。
}
});
}
}
uj5u.com熱心網友回復:
你應該使用MultiPart帖子方法。看看這個.
uj5u.com熱心網友回復:
你可以用這種方式簡單地傳遞圖片檔案:
//header
var headers = {
'content-type': 'multipart/form-data',
'接受'。'application/json',
};
//設定請求。
var request = http.MultipartRequest(
"POST", Uri.parse("your api url"))。)
//向請求主體添加檔案。
request.files.add(await http.MultipartFile.fromPath(
'your feild name',
pickedFile.path,
));
//add header
request.headers.addAll(headers)。
//send request and return responsetry {
var streamedResponse = await request.send()。
var response = http.Response.fromStream(streamedResponse)。
return response;
} catch (e) {
rethrow;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/309246.html
標籤:
上一篇:如何在flutter中根據這些檔案中的特定值來檢索Firebasefirestore中的檔案?
下一篇:<p>我想只滾動自定義步進頁面的底部,但我得到了錯誤。 我無法修復這個錯誤,請幫助我。 我想只滾動自定義步槍頁面的底部,但我得到了錯誤。 我不能修復這個錯誤,請幫助我。 我想只滾動自定義步
