【问题标题】:Google Dart HTTP Error CheckingGoogle Dart HTTP 错误检查
【发布时间】:2014-08-06 16:28:50
【问题描述】:

我正在尝试使用以下教程检测是否发生连接被拒绝错误:https://www.dartlang.org/dart-by-example/#http-requests-and-responses;但是,我继续收到未找到方法的异常,并且无论我的 HTTP 响应是否正常工作,都无法收到任何反馈。任何想法,将不胜感激。如果您需要更多信息,请告诉我。 --忽略“shroot”。

handleFailure(error) {
    print('Something went wrong.');
    print(error.message);
  }

  void loadData(String url) {

    //url = "${baseRestfulUrl}"+ QUERY_ALL_TARGET_ASSETS_BASE_URL;
    print(url);
    //call the web server asynchronously
    var request = HttpRequest.getString(url).then(onDataLoaded);
      request.catchError(handleFailure);

    if (request == null) {
      var warn = (shroot.querySelector('#warning')
          ..text = "No records could be found to match the search criteria."
          ..style.color = "Red");

      ButtonElement resetBtn = shroot.querySelector("#reset-btn");
      resetBtn.disabled = true; 

    } 
    else if(request == 404)
    {
      var warn = (shroot.querySelector('#warning')
               ..text = "Error serving data.  Please restart server."
               ..style.color = "Red");
    }
    else if(request == "ERR_CONNECTION_REFUSED")
    {
      var warn = (shroot.querySelector('#warning')
                    ..text = "Error serving data.  Please restart server."
                    ..style.color = "Red");
    }
    else if(request != null)
    {
      var warn = (shroot.querySelector('#warning')
               ..text = ""
               ..style.color = "#fff");

      ButtonElement resetBtn = shroot.querySelector("#reset-btn");
            resetBtn.disabled = false;
    }
    else {
      var warn = (shroot.querySelector('#warning')
                          ..text = "Error serving data.  Please restart server."
                          ..style.color = "Red");

      ButtonElement resetBtn = shroot.querySelector("#reset-btn");
      resetBtn.disabled = false; 
    }
  }

【问题讨论】:

    标签: dart dart-html


    【解决方案1】:

    您添加的链接是关于Dart I/O and Command Line Apps。这不适用于浏览器中的 Dart。 Dart 必须委托给功能有限的浏览器 API,因此存在差异。

    在发生错误的情况下,ProgressEvent 的实例将传递给此方法

    handleFailure(error) {
      print('Something went wrong.');
      // print(error.message); // ProgressEvent doesn't have a `message` getter
      // the progress event doesn't provide any further information
    
      if(error is ProgressEvent && (error as ProgressEvent).type == 'error') {
        print('An error has occured'); // as we already know because `handleFailure()` was called
      }
    }
    

    这段代码

    if (request == null) {
    

    实际上是在发送请求之前调用的,因为它不在.then

    var request = HttpRequest.getString(url).then(onDataLoaded);
    request.catchError(handleFailure)
    .then((e) {
      if(request == null) { // doesn't make sense because you just assigned a value to request
        ...
      }
    });
    

    【讨论】:

      猜你喜欢
      • 2021-06-03
      • 2021-04-24
      • 1970-01-01
      • 1970-01-01
      • 2019-01-04
      • 1970-01-01
      • 1970-01-01
      • 2021-09-16
      • 2020-07-17
      相关资源
      最近更新 更多