首页 > 解决方案 > Flutter Dio 在 Laravel 服务器上发送空白请求

问题描述

颤振代码:

普通数据 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 对象。FormData 对象包含地图中的所有参数和fields地图中的附件files,我无法在服务器上接收。

表单数据对象 1

表单数据对象 2

在(Laravel)服务器上,由于 laravel 验证失败,我没有从请求中获取任何参数或文件。我搜索了Dio 包文档,但没有找到任何帮助。我们需要处理 Dio 请求,所以如果有人在过去遇到此错误,请提供帮助。谢谢。

标签: laravelflutterlaravel-7dio

解决方案


推荐阅读