HttpRequest.request not working

Issue

I’m creating a wrapper class for Http GET requests, but when I use HttpRequest.request, I don’t get any response. If I use an equivalent code using raw HttpRequests it works.

Am I missing something? I’m using Dart SDK 0.8.10.3_r29803.

class AsyncGet<Output> {
    Future<Output> request(String url, Output process(String response)) {
        // The following works
        var request = new HttpRequest();
        request.open("GET", url);
        request.send(null);
        request.onReadyStateChange.listen((_) {
            if (request.readyState == HttpRequest.DONE &&
                (request.status == 200)) {
                Output out = process(request.responseText);
                _completer.complete(out);
            }
        });

        // The following doesn't work ("Blah" doesn't get logged)
        // HttpRequest.request(url, method: 'GET').then((r) {
        //  Logger.root.info("Blah");
        //  if (r.readyState == HttpRequest.DONE &&
        //      (r.status == 200)) {
        //      Output out = process(request.responseText);
        //      _completer.complete(out);
        //  }
        // })
        return _completer.future;
    }

    Completer<Output> _completer = new Completer<Output>();
}

Solution

Seems like I forgot the semicolon at the end…The following is now working.
It’d be nice if I got some kind of error message for syntax errors.

HttpRequest.request(url).then((r) {
    if (r.readyState == HttpRequest.DONE &&
        (r.status == 200)) {
        Output out = process(r.responseText);
        _completer.complete(out);
    }
});

Answered By – rillomas

Answer Checked By – Robin (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *