简体   繁体   中英

Could not upload image with Dio

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);
 }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM