Flutter Dio在Laravel服务器上发送空白请求

时间:2020-10-06 07:28:07

标签: laravel flutter laravel-7 dio

Flutter代码:

普通数据JSON对象

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

Dio请求的FormData

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

将发出Dio Post请求的功能

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

通话请求

var response = await raiseComplaint(formData, data);

问题: 如果我发送普通的json对象,则来自raiseComplaint()函数,表示正在接收所有参数。我也需要附件文件,因此使用Dio FormData对象。我无法在服务器上接收到的fields映射中的所有参数和files映射中的附件的FormData对象。

FormData Object 1

FormData Object 2

在(Laravel)服务器上,由于laravel验证失败,我没有从请求中获取任何参数或文件。我搜索了Dio package documentation,但没有找到太多帮助。我们需要按照Dio的要求提出建议,所以如果以前有人遇到此错误,请寻求帮助。谢谢。

0 个答案:

没有答案