【问题标题】:Flutter - Always getting wrong POST 400 response before getting the expected 200Flutter - 在得到预期的 200 之前总是得到错误的 POST 400 响应
【发布时间】:2020-02-24 11:12:22
【问题描述】:

我在尝试在 Flutter 中获得对 POST 请求的正确响应时遇到了一些麻烦。这是我写的方法:

  Future<void> processUnload(String code) async {
    Map info = {
      'code': "$code",
    };

    try {
      var response = await http.post(
        url + "/unload/code",
        body: json.encode(info), headers: {
          "Content-Type": "application/json",
          "Authorization": "Bearer $token"
        }
      );

      if (response.statusCode == 200) {
        showUnloadOKMessage();
      } else {
        showUnloadNotOKMessage(response.body);
      }

    }catch (error) {

      if (error.toString().contains("Failed host lookup")) {
        notDelivered.add(OfflineCart(code, DateTime.now().toString()));
        showOfflineUnloadMessage();
      }

    }    
  }

  showUnloadOKMessage() {    
    return showDialog<void>(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context) {
        return AlertDialog(
          title:
              Text('Unload OK.', style: TextStyle(color: Colors.green)),
          actions: <Widget>[
            FlatButton(
              child: Text('OK', style: botonGrandeRojo),
              onPressed: () {
                Navigator.of(context).pop();
                FocusScope.of(contextobuild).requestFocus(focusNode);
              },
            ),
          ],
        );
      },
    );
  }

  showUnloadNotOKMessage(String msg) {
    return showDialog<void>(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text(msg, style: TextStyle(color: Colors.red)),
          actions: <Widget>[
            FlatButton(
              child: Text('OK', style: botonGrandeRojo),
              onPressed: () {
                Navigator.of(context).pop();
                FocusScope.of(contextobuild).requestFocus(focusNode);
              },
            ),
          ],
        );
      },
    );
  }  

问题是,当发出 POST 请求时,我总是在得到实际和预期的 response.statusCode == 200 之前得到 4 或 5 个 response.statusCode == 400。因此,应用程序总是显示 4 或 5" Not OK 消息”,然后显示预期的“Unload OK”消息。

我已经阅读了很多 async/await/then 文章(Threading in FlutterUsing Futures Guide for BeginnersFlutter Async Loader),我很确定问题出在我编写代码的方式上,但我无法让它正常工作,因为我没有任何使用异步调用的经验。

谢谢。

【问题讨论】:

  • 您发布的网址是什么?与curl 的行为相同吗?

标签: asynchronous flutter dart http-post


【解决方案1】:

我发现了错误。问题是在 setState() 中多次调用 processUnload 方法。所以,我只需要添加一个标志来防止它同时被多次调用并删除 setState():

  Widget createCodeText() {
    bool processing = false;

    return Expanded(
      child: TextField(
        autofocus: true,
        decoration: InputDecoration(hintText: 'Esperando lectura...'),
        showCursor: false,
        controller: controllerCode,
        focusNode: focusNode,

        onChanged: (code) async {
          if (!processing){
            processing = true;
            await processUnload (code);
            processing = false;
            controllerCode.clear();
          }
        },

      ),
    );
  }

【讨论】:

    猜你喜欢
    • 2012-12-10
    • 2017-10-09
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-03
    相关资源
    最近更新 更多