Search code examples
dartdart-html

Dart HttpRequest return future


I discovered Dart and developed a Rest Server with it. My understanding of the future is a cool async callback function but I didn't understand how it works to write in the data from a response.

I'm not talking about the client-side, my code look like this:

  getData(HttpRequest request) {
    dao.findAll().then((value) {
      print(value);
    });
  }

The value is printed correctly, now how can I return it in request.response.write?

Thank you in advance for your answers.


Solution

  • You can use request.response.write inside the value handler :

    getData(HttpRequest request) {
      dao.findAll().then((value) {
        request.response
          ..statusCode = HttpStatus.OK
          ..write(value)
          ..close()
          ;
      });
    }