GraphQL Flutter Upload File Not Working - Converting object to an encodable object failed: Instance of 'MultipartFile'

177 Views Asked by At

I am trying to upload a file for my given Graphql mutation in Flutter.

static String uploadSelfieMutation = r'''
        mutation uploadPLSelfie($loanId: String!, $file: Upload!) {
            uploadPLSelfie(loanId: $loanId, file: $file) {
                success
            }
        }
      ''';

// Upload file Code

Future<void> uploadSelfie(String selfiePath, String loanId) async {
    String fileName = selfiePath.split('/').last;
    final mimeTypeData = lookupMimeType(
      selfiePath,
      headerBytes: [0xFF, 0xD8],
    )!
        .split('/');
    final MutationOptions mutationOptions = MutationOptions(
      document: gql(UserMutation.uploadSelfieMutation),
      variables: <String, dynamic>{
        'loanId': loanId,
        'file': await MultipartFile.fromFile(selfiePath,
            filename: fileName,
            contentType: MediaType(mimeTypeData[0], mimeTypeData[1])),
      },
    );
    var result = await locator<GQLHelper>().client.mutate(mutationOptions);
    debugPrint("#uploadSelfie>>> $result");
  }

Getting Below Error:-

QueryResult(source: QueryResultSource.network, data: null, context: Context({}), exception: OperationException(linkException: RequestFormatException(originalException: Converting object to an encodable object failed: Instance of 'MultipartFile', originalStackTrace: #0      _JsonStringifier.writeObject (dart:convert/json.dart:794:7)

Not sure what's wrong here, everywhere I am getting the same way of doing it. Community, please help me to resolve this.

0

There are 0 best solutions below