linkPostRequestWithFile<T extends Link> method
Implementation
Future<RequestResult<T>> linkPostRequestWithFile<T extends Link>(T link,
{GlobalKey<ScaffoldState>? globalKey,
Function()? errorCallback,
int timeout = 7,
String fileFiled = "files",
String dataField = "json",
List<File>? files}) async {
http.StreamedResponse response;
try {
MultipartRequest request =
new http.MultipartRequest('POST', Uri.parse(serverUrl));
request.fields[dataField] = jsonEncode(link.getSendJSON());
if (files != null) {
for (File file in files) {
request.files
.add(await http.MultipartFile.fromPath(fileFiled, file.path));
}
}
response = await request.send();
} on Exception catch (e) {
Debug.debugging("HttpPost", "http request timeout $e");
_showNetErrSnackBar(globalKey);
if (errorCallback != null) errorCallback();
return RequestResult(false, link);
}
if (response.statusCode == 200) {
String bodyString = await response.stream.bytesToString();
try {
var json = jsonDecode(bodyString);
link.receiveJSON(json);
return RequestResult(true, link,
result: json[BKConstants.BK_WORKING_RESULT]);
} catch (e, stack) {
String errorMessage =
"HttpRequest receive error, error message:$e, response body:$bodyString"
" link$link";
Debug.debugging("HttpRequest", errorMessage);
Debug.debugging("HttpRequest", stack.toString());
_showNetErrSnackBar(globalKey);
}
} else {
_showNetErrSnackBar(globalKey);
Debug.debugging("HttpRequest", "network error");
}
return RequestResult(false, link);
}