Flutter Dio sending blank request on Laravel Server

889 views Asked by At

Flutter Code:

Normal Data JSON object

var data = {
    'society_id': widget.societyId,
    'name': _currentCategory,
    'details': txtDescription.text,
    'type': "Society Residential Complaint"
};

FormData for Dio Request

FormData formData = FormData.fromMap({
    'society_id': widget.societyId,
    'name': _currentCategory,
    'details': txtDescription.text,
    'type': "Society Residential Complaint"
});
if (selectedFile != null) {
    String path = selectedFile.path;
    String imagename = selectedFile.path.split('/').last;
    print("Photo Selected");
    formData.files.add(
      MapEntry(
        "photos[]",
        await MultipartFile.fromFile(
          path,
          filename: imagename.toString(),
        ),
      ),
    );
}

Function which will make Dio Post Request

Future raiseComplaint(FormData formData, data) async {
    await _getToken();
    String requestUrl = serverURL + '/complaint/add';
    print("Sending Req: $requestUrl");
    print("--- complaint details: $formData");
    print("--- complaint details: $data");

    try {
      dio.options.headers['Content-Type'] = 'application/json';
      dio.options.headers["Authorization"] = "Bearer $_accessToken";
      Response serverResponse = await dio.post(requestUrl, data: formData);
      var response = _returnResponse(serverResponse);
      print("Recieving Res: $requestUrl");
      print(response);
      return response;
    } catch (e) {
      throw e;
    }
}

Call Request

var response = await raiseComplaint(formData, data);

Problem: from raiseComplaint() function if I send normal json object, I'm receiving all the parameters. I need attachment files as well, so using the Dio FormData object. FormData object wrapping all the parameters in fields map and attachments in files map, which I am unable to receive on server.

FormData Object 1

FormData Object 2

On (Laravel) server I am not getting any parameters or files from the request due to laravel validation fails. I searched the Dio package documentation but didn't found any much help. We need to move with the Dio request, so please if anyone stuck with this error in past, help appriciated. Thanks.

0

There are 0 answers