Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MultipartFile Upload: Converting object to an encodable object failed: Instance of 'FormData' #869

Closed
1 task done
lllleow opened this issue Jul 22, 2020 · 22 comments
Closed
1 task done

Comments

@lllleow
Copy link

lllleow commented Jul 22, 2020

New Issue Checklist

  • I have searched for a similar issue in the project and found none

Issue Info

Info Value
Platform Name Flutter
Platform Version 1.17.2
Dio Version 3.0.9
Android Studio / Xcode Version VSCode / Xcode 11
Repro rate 100%

Issue Description and Steps

Hello, i am trying to upload an image to a server. I've used the guides provided in the documentation to get the process started.

The error message that i am receiving is:
DioError [DioErrorType.DEFAULT]: Converting object to an encodable object failed: Instance of 'FormData'

My code is:
image

image

@stale
Copy link

stale bot commented Aug 21, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, please make sure it is up to date and if so, add a comment that this is still an issue to keep it open. Thank you for your contributions.

@stale stale bot added the stale label Aug 21, 2020
@yangtao2016
Copy link

我也碰到了相同的问题:Converting object to an encodable object failed: Instance of 'FormData

@stale stale bot removed the stale label Aug 27, 2020
@stale
Copy link

stale bot commented Sep 26, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, please make sure it is up to date and if so, add a comment that this is still an issue to keep it open. Thank you for your contributions.

@stale stale bot added the stale label Sep 26, 2020
@stale stale bot closed this as completed Oct 4, 2020
@ejrgeek
Copy link

ejrgeek commented Oct 12, 2020

I'm having the same error:

Code:

MultipartFile file = await MultipartFile.fromFile(image.path);
responseDio = await dio.request(
    endpointUpdateImageProfile,
    options: Options(
    method: "POST",
        headers: {
            'Content-Type': 'multipart/form-data',
            'Authorization': 'token $token',
        },
    ),
    data: {'profile': file},
);

Error:

ERRO: DioError [DioErrorType.DEFAULT]: Converting object to an encodable object failed: Instance of 'MultipartFile'
I/flutter ( 6127): #0      _JsonStringifier.writeObject  (dart:convert/json.dart:687:7))
I/flutter ( 6127): #1      _JsonStringifier.writeMap  (dart:convert/json.dart:768:7))
I/flutter ( 6127): #2      _JsonStringifier.writeJsonValue  (dart:convert/json.dart:723:21))
I/flutter ( 6127): #3      _JsonStringifier.writeObject  (dart:convert/json.dart:678:9))
I/flutter ( 6127): #4      _JsonStringStringifier.printOn  (dart:convert/json.dart:876:17))
I/flutter ( 6127): #5      _JsonStringStringifier.stringify  (dart:convert/json.dart:861:5))
I/flutter ( 6127): #6      JsonEncoder.convert  (dart:convert/json.dart:261:30))
I/flutter ( 6127): #7      JsonCodec.encode  (dart:convert/json.dart:171:45))
I/flutter ( 6127): #8      DefaultTransformer.transformRequest 
package:dio/src/transformer.dart:62
I/flutter ( 6127): #9      DioMixin._transformData 
package:dio/src/dio.dart:1014
I/flutter ( 6127): #10     DioMixin._dispatchRequest 
package:dio/src/dio.dart:921
I/flutter ( 6127): #11     DioMixin._request._interceptorWrapper.<anonymous closure>.<anonymous closure>.<anonymous closure> 
package:dio/src/dio.dart:849
I/flutter ( 6127): #12     DioM

dioerror

@chuongdevJC
Copy link

Did anyone resolve this issue pls ?

@Jagrajsinghji
Copy link

Try adding MediaType

MultipartFile mFile = await MultipartFile.fromFile(
      photo.path, filename: fileName,
      contentType: MediaType("image", fileName.split(".").last),);

The MediaType class is defined in http_parser, so make sure you use ths package

@pedrolacerda81
Copy link

Try adding MediaType

MultipartFile mFile = await MultipartFile.fromFile(
      photo.path, filename: fileName,
      contentType: MediaType("image", fileName.split(".").last),);

The MediaType class is defined in http_parser, so make sure you use ths package

@Jagrajsinghji added and still same error

@kateile
Copy link

kateile commented Apr 3, 2021

same error. Any solution

@ahmedalrifai
Copy link

Any solution am having the same issue?

@MarcoGolin
Copy link

same error. Any solution? =(

@Jagrajsinghji
Copy link

Could anyone of you share you code snippet?

@MarcoGolin
Copy link

Could anyone of you share you code snippet?

solved, I wasn't using the FormData correctly, trying to send as Json

@abdelrahmanelmarakby
Copy link

Could anyone of you share you code snippet?

solved, I wasn't using the FormData correctly, trying to send as Json

tell us how did you solved this

@secret3579
Copy link

Could anyone of you share you code snippet?

solved, I wasn't using the FormData correctly, trying to send as Json

Please give your code example for this issue?

@tinkokowin96
Copy link

tinkokowin96 commented Aug 21, 2022

hope this help

     final file = await dio.MultipartFile.fromFile(
        _image?.path ?? "",
        filename: _image?.name,
      );

      final body = dio.FormData.fromMap({
        "email": _emailController.text,
        "password": _passwordController.text,
        "name": _nameController.text,
        "phone": _phoneController.text,
        "file": file
      });

@icemanbsi
Copy link

I have similar problem and I think I know the answer. We are looking at the wrong place.
The issue is triggered by jsonEncode function which trying to encode FormData instance. Since the FormData cannot be encoded to json string, it will trigger this error.

So the next step is find out where you place your debug print or similar function which called the jsonEncode. It might be on your interpreter too.

@devmemory
Copy link

I have similar problem and I think I know the answer. We are looking at the wrong place. The issue is triggered by jsonEncode function which trying to encode FormData instance. Since the FormData cannot be encoded to json string, it will trigger this error.

So the next step is find out where you place your debug print or similar function which called the jsonEncode. It might be on your interpreter too.

Thanks for icemanbsi. I could figure out why I faced this error.
According to what is written here, the problem was where I print whenever I call an API. And to see the request options without debugging mode, I'm configuring some parts.
And one of them was where I encode requestOptions.data. And after I blocked that part conditionally, I could solve it perfectly. :)

@zzz1029335886
Copy link

The problem occurred on log printing json.encode the data extends InterceptorsWrapper

@sulthanalihsan
Copy link

The problem occurred on log printing json.encode the data extends InterceptorsWrapper

how you solve that problem?

@Oliul-Hasnat-Rafi
Copy link

FormData data = FormData.fromMap({
'key': 'f8b0266d23d2f048bb5.',
'image': await MultipartFile.fromFile(filePath, filename: filename)
});
var response =
await dio.post('https://api.imgbb.com/1/upload', data: data);

it's work for me

@dakshatsuraasa
Copy link

same issue no solution

@MalikAbdMusleh
Copy link

Could anyone of you share you code snippet?

solved, I wasn't using the FormData correctly, trying to send as Json

worked thanks ♥ ♥

@cfug cfug locked and limited conversation to collaborators Jun 30, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests