uploadImageToImgur method
Future<ImgurUploadData?>
uploadImageToImgur({
- required XFile file,
- GeneralCallback<
ImgurUploadData?> ? callback,
Implementation
Future<ImgurUploadData?> uploadImageToImgur({
required XFile file,
GeneralCallback<ImgurUploadData?>? callback,
}) async {
try {
final Uint8List bytes = await file.readAsBytes();
final Response<Map<String, dynamic>> response = await dio.post(
'/3/image',
options: Options(
headers: <String, String>{
'Authorization': 'Client-ID $clientId',
},
),
data: FormData.fromMap(
<String, dynamic>{
'image': MultipartFile.fromBytes(
bytes,
filename: p.split(file.path).last,
),
},
),
);
if (response.statusCode == 200) {
final ImgurUploadResponse imgurUploadResponse =
ImgurUploadResponse.fromJson(response.data!);
return callback == null
? imgurUploadResponse.data
: callback.onSuccess(imgurUploadResponse.data) as ImgurUploadData;
} else {
callback?.onError(
GeneralResponse(
statusCode: 201,
message:
response.statusMessage ?? ApLocalizations.current.unknownError,
),
);
return null;
}
} on DioException catch (dioException) {
if (dioException.type == DioExceptionType.badResponse &&
dioException.response?.statusCode == 400) {
callback?.onError(
GeneralResponse(
statusCode: 201,
message: ApLocalizations.current.notSupportImageType,
),
);
} else {
callback?.onFailure(dioException);
}
return null;
}
}