Could not upload image with Dio

153 views Asked by At

I'm using Dio to upload images to backend. It often gives me the Reference not set response with a 500 error code. I tried uploading from another source and it seems to be working. What's wrong with this code?

Haven't put the code for performPostRequestWithToken() because other methods are using it too and it seems to be working alright.

Future<UserModel> submitProfileImage(String imagePath) async {
 if (isEmpty(imagePath)) throw Exception("NULL image found");
 final formdata = FormData.fromMap({
   "profilePic": await MultipartFile.fromFile(
     imagePath,
     filename: "profilePic.png",
   ),
 });
 final usertoken = await getCurrentUserToken();
 print(usertoken);
 final response = await _dioHttpService.performPostRequestWithToken(
   "/User/UploadImage",
   formdata,
   usertoken,
 );
 if (response.statusCode >= 200 && response.statusCode < 300) {
   return UserModel.fromMap(response.data["data"]);
 } else {
   throw Exception(response.statusMessage);
 }
}
1

There are 1 answers

0
Aditya Nigam On

The problem in my case was that the map key profilePic was causing the issues. This was definitely a problem from the backend because they were expecting the key to be file. Changed that and it all worked out.