【问题标题】:Line after await http response in Flutter app does not executeFlutter 应用程序中等待 http 响应后的行不执行
【发布时间】:2021-05-06 17:08:57
【问题描述】:

我想在向服务器发送 http.MultipartFile 请求后从 Flask API 获取结果。 像这样:

Future<String> upload(List<int> filename, String url) async {
            //filename : binary conversion of string
            //String url : api address
  print("Inside Upload future!");
  var request = http.MultipartRequest('POST', Uri.parse(url));
  request.files.add(
      http.MultipartFile.fromBytes('file', filename, filename: 'files.txt'));
  print("Sending Request!");
  http.Response response = await http.Response.fromStream(await request.send());
  print("request sent! now returning");
  var rtr = response.body;
  return rtr;
}

但问题是它不返回,执行后的输出是 The print after await is not executed 为什么??:

Inside Upload future!
Sending Request!

我正在向 Flask API 发送一个字符串,例如: 它工作正常,它正在接收一个字符串,然后用相同的字符串进行一些修改。

@app.route('/test_api', methods=['GET', 'POST'])
def test_api():
 uploaded_file = request.files['file']
 file_content = uploaded_file.read().splitlines()
 uploaded_file.seek(0)
 file_pretty = uploaded_file.read()
 a = runkey(file_pretty)
     //takes string agrument and returns manipulated string
 uploaded_file.seek(0)
 filename = secure_filename(uploaded_file.filename)
 resp = make_response(a)
 resp.headers['Content-Type'] = 'text/plain;charset=UTF-8'
 n = filename
 resp.headers['Content-Disposition'] = 'attachment;filename='+'n'
 return resp ```

【问题讨论】:

    标签: flutter dart asynchronous


    【解决方案1】:

    我通过添加解决了这个问题

    resp.headers['Access-Control-Allow-Origin'] = '*'
    //before returning response to client
    

    到 FlaskAPI 响应。

    实际上,我认为 Dart 的某个地方有错误。但它隐藏在 Chrome 调试中,因为我使用的是 Flutter Web。 错误是由于访问控制而不允许跨域。

    【讨论】:

      猜你喜欢
      • 2017-06-30
      • 1970-01-01
      • 2018-11-18
      • 2016-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-05
      相关资源
      最近更新 更多