Dart Future HttpCLientRequest Returns Null









up vote
0
down vote

favorite












The class Requests set up an HttpClientRequest. The method getTeamsJsonForRequest is supposed to return the JSON response. However, the variable 'return' is not being assigned properly I assum. The print 'CONTS' in the .then response successfully prints the correct response, but printing 'myres' sections says result is null. Not sure why result is not being assigned in the response.transform section.



class Requests 
static Future getTeamsJsonForRequest(String reqPath) async
var result;
HttpClient myhttp = new HttpClient();
String path = '/api/v3' + reqPath;
myhttp.get('www.thebluealliance.com', 80, path)
.then((HttpClientRequest request)
request.headers.set("accept", "application/json");
request.headers.set("X-TBA-Auth-Key", "XXXXX");
return request.close();
)
.then((HttpClientResponse response)
response.transform(utf8.decoder).transform(json.decoder).listen((conts)
print('CONTS: ' + conts.toString());
result = json.decode(conts).toString();
);
);
print('myres: ' + result.toString());
return result;











share|improve this question

























    up vote
    0
    down vote

    favorite












    The class Requests set up an HttpClientRequest. The method getTeamsJsonForRequest is supposed to return the JSON response. However, the variable 'return' is not being assigned properly I assum. The print 'CONTS' in the .then response successfully prints the correct response, but printing 'myres' sections says result is null. Not sure why result is not being assigned in the response.transform section.



    class Requests 
    static Future getTeamsJsonForRequest(String reqPath) async
    var result;
    HttpClient myhttp = new HttpClient();
    String path = '/api/v3' + reqPath;
    myhttp.get('www.thebluealliance.com', 80, path)
    .then((HttpClientRequest request)
    request.headers.set("accept", "application/json");
    request.headers.set("X-TBA-Auth-Key", "XXXXX");
    return request.close();
    )
    .then((HttpClientResponse response)
    response.transform(utf8.decoder).transform(json.decoder).listen((conts)
    print('CONTS: ' + conts.toString());
    result = json.decode(conts).toString();
    );
    );
    print('myres: ' + result.toString());
    return result;











    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      The class Requests set up an HttpClientRequest. The method getTeamsJsonForRequest is supposed to return the JSON response. However, the variable 'return' is not being assigned properly I assum. The print 'CONTS' in the .then response successfully prints the correct response, but printing 'myres' sections says result is null. Not sure why result is not being assigned in the response.transform section.



      class Requests 
      static Future getTeamsJsonForRequest(String reqPath) async
      var result;
      HttpClient myhttp = new HttpClient();
      String path = '/api/v3' + reqPath;
      myhttp.get('www.thebluealliance.com', 80, path)
      .then((HttpClientRequest request)
      request.headers.set("accept", "application/json");
      request.headers.set("X-TBA-Auth-Key", "XXXXX");
      return request.close();
      )
      .then((HttpClientResponse response)
      response.transform(utf8.decoder).transform(json.decoder).listen((conts)
      print('CONTS: ' + conts.toString());
      result = json.decode(conts).toString();
      );
      );
      print('myres: ' + result.toString());
      return result;











      share|improve this question













      The class Requests set up an HttpClientRequest. The method getTeamsJsonForRequest is supposed to return the JSON response. However, the variable 'return' is not being assigned properly I assum. The print 'CONTS' in the .then response successfully prints the correct response, but printing 'myres' sections says result is null. Not sure why result is not being assigned in the response.transform section.



      class Requests 
      static Future getTeamsJsonForRequest(String reqPath) async
      var result;
      HttpClient myhttp = new HttpClient();
      String path = '/api/v3' + reqPath;
      myhttp.get('www.thebluealliance.com', 80, path)
      .then((HttpClientRequest request)
      request.headers.set("accept", "application/json");
      request.headers.set("X-TBA-Auth-Key", "XXXXX");
      return request.close();
      )
      .then((HttpClientResponse response)
      response.transform(utf8.decoder).transform(json.decoder).listen((conts)
      print('CONTS: ' + conts.toString());
      result = json.decode(conts).toString();
      );
      );
      print('myres: ' + result.toString());
      return result;








      json http dart






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 9 at 20:43









      yzet00

      166318




      166318






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          Short answer: avoid using Future.then inside an async method.



          Your print is executing before the response comes back. https://www.dartlang.org/tutorials/language/futures#async-await



          Without an await any work that is done asynchronously will happen after subsequent statements in this function are executed.



          Here is how I'd write this:



          Future<String> teamsJsonForRequest(String requestPath) async 
          var client = new HttpClient();
          var path = '/api/v3$requestPath';
          var request = (await client.get('www.thebluealliance.com', 80, path))
          ..headers.set("accept", "application/json")
          ..headers.set("X-TBA-Auth-Key", "XXXXX");
          var response = await request.close();
          var result =
          await response.transform(utf8.decoder).transform(json.decoder).single;
          print('myres: $result');
          return result;






          share|improve this answer



























            up vote
            0
            down vote













            In



             .then((HttpClientResponse response) 
            response.transform(utf8.decoder).transform(json.decoder).listen((conts)
            print('CONTS: ' + conts.toString());
            result = json.decode(conts).toString();
            );
            );
            print('myres: ' + result.toString());
            return result;


            this line



            result = json.decode(conts).toString();


            is executed muuuch later than this line



            return result;



            The request to the server is sent and then return result; is executed.
            The then(...) part is executed when the response from the server arrives.



            Change the code to



            class Requests 
            static Future getTeamsJsonForRequest(String reqPath) async
            HttpClient myhttp = new HttpClient();
            String path = '/api/v3' + reqPath;
            return myhttp.get('www.thebluealliance.com', 80, path)
            .then((HttpClientRequest request)
            request.headers.set("accept", "application/json");
            request.headers.set("X-TBA-Auth-Key", "XXXXX");
            return request.close();
            )
            .then((HttpClientResponse response)
            response.transform(utf8.decoder).transform(json.decoder).listen((conts)
            print('CONTS: ' + conts.toString());
            var result = json.decode(conts).toString();
            print('myres: ' + result.toString());
            return result;
            );
            );







            share|improve this answer




















              Your Answer






              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "1"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













               

              draft saved


              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233008%2fdart-future-httpclientrequest-returns-null%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              0
              down vote



              accepted










              Short answer: avoid using Future.then inside an async method.



              Your print is executing before the response comes back. https://www.dartlang.org/tutorials/language/futures#async-await



              Without an await any work that is done asynchronously will happen after subsequent statements in this function are executed.



              Here is how I'd write this:



              Future<String> teamsJsonForRequest(String requestPath) async 
              var client = new HttpClient();
              var path = '/api/v3$requestPath';
              var request = (await client.get('www.thebluealliance.com', 80, path))
              ..headers.set("accept", "application/json")
              ..headers.set("X-TBA-Auth-Key", "XXXXX");
              var response = await request.close();
              var result =
              await response.transform(utf8.decoder).transform(json.decoder).single;
              print('myres: $result');
              return result;






              share|improve this answer
























                up vote
                0
                down vote



                accepted










                Short answer: avoid using Future.then inside an async method.



                Your print is executing before the response comes back. https://www.dartlang.org/tutorials/language/futures#async-await



                Without an await any work that is done asynchronously will happen after subsequent statements in this function are executed.



                Here is how I'd write this:



                Future<String> teamsJsonForRequest(String requestPath) async 
                var client = new HttpClient();
                var path = '/api/v3$requestPath';
                var request = (await client.get('www.thebluealliance.com', 80, path))
                ..headers.set("accept", "application/json")
                ..headers.set("X-TBA-Auth-Key", "XXXXX");
                var response = await request.close();
                var result =
                await response.transform(utf8.decoder).transform(json.decoder).single;
                print('myres: $result');
                return result;






                share|improve this answer






















                  up vote
                  0
                  down vote



                  accepted







                  up vote
                  0
                  down vote



                  accepted






                  Short answer: avoid using Future.then inside an async method.



                  Your print is executing before the response comes back. https://www.dartlang.org/tutorials/language/futures#async-await



                  Without an await any work that is done asynchronously will happen after subsequent statements in this function are executed.



                  Here is how I'd write this:



                  Future<String> teamsJsonForRequest(String requestPath) async 
                  var client = new HttpClient();
                  var path = '/api/v3$requestPath';
                  var request = (await client.get('www.thebluealliance.com', 80, path))
                  ..headers.set("accept", "application/json")
                  ..headers.set("X-TBA-Auth-Key", "XXXXX");
                  var response = await request.close();
                  var result =
                  await response.transform(utf8.decoder).transform(json.decoder).single;
                  print('myres: $result');
                  return result;






                  share|improve this answer












                  Short answer: avoid using Future.then inside an async method.



                  Your print is executing before the response comes back. https://www.dartlang.org/tutorials/language/futures#async-await



                  Without an await any work that is done asynchronously will happen after subsequent statements in this function are executed.



                  Here is how I'd write this:



                  Future<String> teamsJsonForRequest(String requestPath) async 
                  var client = new HttpClient();
                  var path = '/api/v3$requestPath';
                  var request = (await client.get('www.thebluealliance.com', 80, path))
                  ..headers.set("accept", "application/json")
                  ..headers.set("X-TBA-Auth-Key", "XXXXX");
                  var response = await request.close();
                  var result =
                  await response.transform(utf8.decoder).transform(json.decoder).single;
                  print('myres: $result');
                  return result;







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 9 at 21:49









                  Nate Bosch

                  58929




                  58929






















                      up vote
                      0
                      down vote













                      In



                       .then((HttpClientResponse response) 
                      response.transform(utf8.decoder).transform(json.decoder).listen((conts)
                      print('CONTS: ' + conts.toString());
                      result = json.decode(conts).toString();
                      );
                      );
                      print('myres: ' + result.toString());
                      return result;


                      this line



                      result = json.decode(conts).toString();


                      is executed muuuch later than this line



                      return result;



                      The request to the server is sent and then return result; is executed.
                      The then(...) part is executed when the response from the server arrives.



                      Change the code to



                      class Requests 
                      static Future getTeamsJsonForRequest(String reqPath) async
                      HttpClient myhttp = new HttpClient();
                      String path = '/api/v3' + reqPath;
                      return myhttp.get('www.thebluealliance.com', 80, path)
                      .then((HttpClientRequest request)
                      request.headers.set("accept", "application/json");
                      request.headers.set("X-TBA-Auth-Key", "XXXXX");
                      return request.close();
                      )
                      .then((HttpClientResponse response)
                      response.transform(utf8.decoder).transform(json.decoder).listen((conts)
                      print('CONTS: ' + conts.toString());
                      var result = json.decode(conts).toString();
                      print('myres: ' + result.toString());
                      return result;
                      );
                      );







                      share|improve this answer
























                        up vote
                        0
                        down vote













                        In



                         .then((HttpClientResponse response) 
                        response.transform(utf8.decoder).transform(json.decoder).listen((conts)
                        print('CONTS: ' + conts.toString());
                        result = json.decode(conts).toString();
                        );
                        );
                        print('myres: ' + result.toString());
                        return result;


                        this line



                        result = json.decode(conts).toString();


                        is executed muuuch later than this line



                        return result;



                        The request to the server is sent and then return result; is executed.
                        The then(...) part is executed when the response from the server arrives.



                        Change the code to



                        class Requests 
                        static Future getTeamsJsonForRequest(String reqPath) async
                        HttpClient myhttp = new HttpClient();
                        String path = '/api/v3' + reqPath;
                        return myhttp.get('www.thebluealliance.com', 80, path)
                        .then((HttpClientRequest request)
                        request.headers.set("accept", "application/json");
                        request.headers.set("X-TBA-Auth-Key", "XXXXX");
                        return request.close();
                        )
                        .then((HttpClientResponse response)
                        response.transform(utf8.decoder).transform(json.decoder).listen((conts)
                        print('CONTS: ' + conts.toString());
                        var result = json.decode(conts).toString();
                        print('myres: ' + result.toString());
                        return result;
                        );
                        );







                        share|improve this answer






















                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          In



                           .then((HttpClientResponse response) 
                          response.transform(utf8.decoder).transform(json.decoder).listen((conts)
                          print('CONTS: ' + conts.toString());
                          result = json.decode(conts).toString();
                          );
                          );
                          print('myres: ' + result.toString());
                          return result;


                          this line



                          result = json.decode(conts).toString();


                          is executed muuuch later than this line



                          return result;



                          The request to the server is sent and then return result; is executed.
                          The then(...) part is executed when the response from the server arrives.



                          Change the code to



                          class Requests 
                          static Future getTeamsJsonForRequest(String reqPath) async
                          HttpClient myhttp = new HttpClient();
                          String path = '/api/v3' + reqPath;
                          return myhttp.get('www.thebluealliance.com', 80, path)
                          .then((HttpClientRequest request)
                          request.headers.set("accept", "application/json");
                          request.headers.set("X-TBA-Auth-Key", "XXXXX");
                          return request.close();
                          )
                          .then((HttpClientResponse response)
                          response.transform(utf8.decoder).transform(json.decoder).listen((conts)
                          print('CONTS: ' + conts.toString());
                          var result = json.decode(conts).toString();
                          print('myres: ' + result.toString());
                          return result;
                          );
                          );







                          share|improve this answer












                          In



                           .then((HttpClientResponse response) 
                          response.transform(utf8.decoder).transform(json.decoder).listen((conts)
                          print('CONTS: ' + conts.toString());
                          result = json.decode(conts).toString();
                          );
                          );
                          print('myres: ' + result.toString());
                          return result;


                          this line



                          result = json.decode(conts).toString();


                          is executed muuuch later than this line



                          return result;



                          The request to the server is sent and then return result; is executed.
                          The then(...) part is executed when the response from the server arrives.



                          Change the code to



                          class Requests 
                          static Future getTeamsJsonForRequest(String reqPath) async
                          HttpClient myhttp = new HttpClient();
                          String path = '/api/v3' + reqPath;
                          return myhttp.get('www.thebluealliance.com', 80, path)
                          .then((HttpClientRequest request)
                          request.headers.set("accept", "application/json");
                          request.headers.set("X-TBA-Auth-Key", "XXXXX");
                          return request.close();
                          )
                          .then((HttpClientResponse response)
                          response.transform(utf8.decoder).transform(json.decoder).listen((conts)
                          print('CONTS: ' + conts.toString());
                          var result = json.decode(conts).toString();
                          print('myres: ' + result.toString());
                          return result;
                          );
                          );








                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 9 at 21:50









                          Günter Zöchbauer

                          306k63901849




                          306k63901849



























                               

                              draft saved


                              draft discarded















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233008%2fdart-future-httpclientrequest-returns-null%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Use pre created SQLite database for Android project in kotlin

                              Darth Vader #20

                              Ondo