uploadFile method
Future
uploadFile(
- dynamic filePath
)
Implementation
Future<dynamic> uploadFile(filePath) async {
//user im use to upload image
// String? token = prefs.getString("token");
// token =
// "JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNjI4MjgyMzIyLCJqdGkiOiIxNTk0OTE4NDc0ZDI0MzhjOWI5NDY2ZTQ2YTg5ZjVlMyIsInVzZXJfaWQiOjE2fQ.Qw7VuCkKuhyCoLoiHAWdzjgkZvSUvxEh26i61ihGdHM";
dynamic returnable;
await SharedPreferences.getInstance().then((pref) async {
String? token = pref.getString("token");
try {
FormData formData = new FormData.fromMap({
"file": await MultipartFile.fromFile(filePath, filename: "image")
});
var jsonData;
Response response = await Dio().post(Params.base_url + "/base/upload",
data: formData,
options: Options(headers: <String, String>{
'Authorization': 'Bearer $token',
}));
if (debug) {
print('Status Code = ' +
response.statusCode.toString() +
". Response: " +
response.data.toString());
}
/*If the response is 200 or 201 (success) then the response will be decoded */
if (response.statusCode == 200 || response.statusCode == 201) {
jsonData = json.decode(response.data.toString());
returnable = {
"status": "success",
"message": jsonData["message"],
"data": jsonData["data"]
};
} else if (response.statusCode == 422) {
jsonData = json.decode(response.data.toString());
returnable = {
"status": "error",
"error": jsonData["message"],
};
} else {
if (debug) print("Error Requesting API");
returnable = null;
}
} on DioError catch (e) {
returnable = e.response;
} catch (e) {}
});
return returnable;
}