Asynchronous 我可以在浏览器中对post请求使用Dart async/await吗?

Asynchronous 我可以在浏览器中对post请求使用Dart async/await吗?,asynchronous,browser,dart,dartium,Asynchronous,Browser,Dart,Dartium,我使用此示例作为开发Dart应用程序的起点,该应用程序使用API端点数据: void saveData() { HttpRequest request = new HttpRequest(); // create a new XHR // add an event handler that is called when the request finishes request.onReadyStateChange.listen((_) { if (request.ready

我使用此示例作为开发Dart应用程序的起点,该应用程序使用API端点数据:

void saveData() {
  HttpRequest request = new HttpRequest(); // create a new XHR

  // add an event handler that is called when the request finishes
  request.onReadyStateChange.listen((_) {
    if (request.readyState == HttpRequest.DONE &&
        (request.status == 200 || request.status == 0)) {
      // data saved OK.
      print(request.responseText); // output the response from the server
    }
  });

  // POST the data to the server
  var url = "http://127.0.0.1:8080/programming-languages";
  request.open("POST", url, async: false);

  String jsonData = '{"language":"dart"}'; // etc...
  request.send(jsonData); // perform the async POST
}
我认为这是一个传统的回调,在发生某些事情时运行。在这里,当接收到响应时执行

不过,我想尝试另一种方法,比如使用Futures/Promises或async/await

是否可以在浏览器中将此示例转换为这些备选方案中的任何一个


如果是这样的话,您能否向示例展示它作为Future或async/await实现时的外观?

我同意@Pacane关于使用该软件包的看法。它为处理
http
请求提供了一个更干净的API,允许您轻松使用async/await

但是,您可以只使用以下核心库编写
saveData
(此处的dartpad示例:)

Future saveData()异步{
var response=wait HttpRequest.postFormData(
'http://127.0.0.1:8080/programming-语言',
{'language':'Dart'});
打印(response.responseText);
}

查看该软件包
http
,它有一个很好的使用期货和流的API。
Future<Null> saveData() async {
  var response = await HttpRequest.postFormData(
      'http://127.0.0.1:8080/programming-languages',
      {'language': 'Dart'});
  print(response.responseText);
}