uploadImage static method
dynamic
uploadImage({})
Implementation
static uploadImage({
required uri,
required File file,
var body,
required String? fileType,
header,
bool hasBearer = false,
required Function onSuccess,
Function? onFailure,
Function? onTimeout,
Function? onConnectionFailed,
}) async {
if (hasBearer) {
header = {
// 'Content-Type': 'application/json',
'secretKey': AppStorages.secKey,
};
}
var url = Global.baseApiUrl + uri;
var request = http.MultipartRequest('POST', Uri.parse(url));
if (hasBearer) request.headers.addAll(header);
request.fields.addAll(body);
if (file.path.isNotEmpty) {
var stream = http.ByteStream(Stream.castFrom(file.openRead()));
var length = await file.length();
var multipartFile =
http.MultipartFile(fileType!, stream, length, filename: basename(file.path));
request.files.add(multipartFile);
}
await request.send().then((resStream) async {
resStream.stream.transform(utf8.decoder).transform(const LineSplitter()).listen((response) {
Log.displayResponse(payload: body, res: response, requestType: 'Upload');
final bodyData = jsonDecode(response);
print(bodyData);
// hideAppLoader();
if (bodyData['success'] ) {
onSuccess(response);
} else {
onFailure!(bodyData['message']);
}
});
}).catchError((error) {
// hideAppLoader();
if (kDebugMode) {
print('Error : catchError $error');
print(url);
}
// getSnackToast(
// message: error.toString(),
// colorText: Colors.white,
// backgroundColor: Colors.red,
// duration: const Duration(seconds: 5));
onFailure!(error);
}).timeout(const Duration(seconds: 20), onTimeout: () {
// hideAppLoader();
if (kDebugMode) {
print(url);
print('Error : TimeOut');
}
if (onTimeout != null) onTimeout();
});
}