How Can I get a List object by HttpClientResponse?












0















How can I get a List object in "result"? I tried with toList() method on "request" object, but I can't solve this.



import 'dart:io';
import 'dart:convert';

void main() async {
var request = await HttpClient().getUrl(Uri.parse('https://jsonplaceholder.typicode.com/posts')); // produces a request object
var response = await request.close(); // sends the request

// transforms and prints the response
var result;

await for (var contents in response.transform(Utf8Decoder())) {
result =contents;
}

print(result);

}


Thanks,



Daniel










share|improve this question



























    0















    How can I get a List object in "result"? I tried with toList() method on "request" object, but I can't solve this.



    import 'dart:io';
    import 'dart:convert';

    void main() async {
    var request = await HttpClient().getUrl(Uri.parse('https://jsonplaceholder.typicode.com/posts')); // produces a request object
    var response = await request.close(); // sends the request

    // transforms and prints the response
    var result;

    await for (var contents in response.transform(Utf8Decoder())) {
    result =contents;
    }

    print(result);

    }


    Thanks,



    Daniel










    share|improve this question

























      0












      0








      0








      How can I get a List object in "result"? I tried with toList() method on "request" object, but I can't solve this.



      import 'dart:io';
      import 'dart:convert';

      void main() async {
      var request = await HttpClient().getUrl(Uri.parse('https://jsonplaceholder.typicode.com/posts')); // produces a request object
      var response = await request.close(); // sends the request

      // transforms and prints the response
      var result;

      await for (var contents in response.transform(Utf8Decoder())) {
      result =contents;
      }

      print(result);

      }


      Thanks,



      Daniel










      share|improve this question














      How can I get a List object in "result"? I tried with toList() method on "request" object, but I can't solve this.



      import 'dart:io';
      import 'dart:convert';

      void main() async {
      var request = await HttpClient().getUrl(Uri.parse('https://jsonplaceholder.typicode.com/posts')); // produces a request object
      var response = await request.close(); // sends the request

      // transforms and prints the response
      var result;

      await for (var contents in response.transform(Utf8Decoder())) {
      result =contents;
      }

      print(result);

      }


      Thanks,



      Daniel







      dart flutter






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 24 '18 at 16:09









      DomDunkDomDunk

      23




      23
























          2 Answers
          2






          active

          oldest

          votes


















          0














          Firstly, response.transform return multiple parts of result, you need to get all by joining them together:



            var result = new StringBuffer();
          await for (var contents in response.transform(Utf8Decoder())) {
          result.write(contents);
          }


          Secondly, you result is just a "text" (in json format), not an "object" as you expect, you need to decode it:



            List<dynamic> myList = jsonDecode(result.toString());
          print(myList);





          share|improve this answer
























          • import 'dart:io'; import 'dart:convert'; void main() async { var request = await HttpClient().getUrl(Uri.parse('earthquake.usgs.gov/earthquakes/feed/v1.0/summary/…)); var response = await request.close(); // sends the request var result = new StringBuffer(); await for (var contents in response.transform(Utf8Decoder())) { result.write(contents); } List<dynamic> myList = jsonDecode(result.toString()); print(myList[0]['title']); }

            – DomDunk
            Nov 27 '18 at 12:41











          • Why does not it work?

            – DomDunk
            Nov 27 '18 at 12:42











          • I solved in this way:

            – DomDunk
            Nov 27 '18 at 18:00











          • 'code' var myList = jsonDecode(result.toString()); List quakes = myList['features']; } 'code'

            – DomDunk
            Nov 27 '18 at 18:01



















          0














          Simply the following



          List elements = json.decode(response.body);





          share|improve this answer
























          • error: The getter 'body' isn't defined for the class 'HttpClientResponse'. (undefined_getter at [flutter_app] libmain.dart:40)

            – DomDunk
            Nov 27 '18 at 2:12











          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',
          autoActivateHeartbeat: false,
          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%2f53459989%2fhow-can-i-get-a-list-object-by-httpclientresponse%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









          0














          Firstly, response.transform return multiple parts of result, you need to get all by joining them together:



            var result = new StringBuffer();
          await for (var contents in response.transform(Utf8Decoder())) {
          result.write(contents);
          }


          Secondly, you result is just a "text" (in json format), not an "object" as you expect, you need to decode it:



            List<dynamic> myList = jsonDecode(result.toString());
          print(myList);





          share|improve this answer
























          • import 'dart:io'; import 'dart:convert'; void main() async { var request = await HttpClient().getUrl(Uri.parse('earthquake.usgs.gov/earthquakes/feed/v1.0/summary/…)); var response = await request.close(); // sends the request var result = new StringBuffer(); await for (var contents in response.transform(Utf8Decoder())) { result.write(contents); } List<dynamic> myList = jsonDecode(result.toString()); print(myList[0]['title']); }

            – DomDunk
            Nov 27 '18 at 12:41











          • Why does not it work?

            – DomDunk
            Nov 27 '18 at 12:42











          • I solved in this way:

            – DomDunk
            Nov 27 '18 at 18:00











          • 'code' var myList = jsonDecode(result.toString()); List quakes = myList['features']; } 'code'

            – DomDunk
            Nov 27 '18 at 18:01
















          0














          Firstly, response.transform return multiple parts of result, you need to get all by joining them together:



            var result = new StringBuffer();
          await for (var contents in response.transform(Utf8Decoder())) {
          result.write(contents);
          }


          Secondly, you result is just a "text" (in json format), not an "object" as you expect, you need to decode it:



            List<dynamic> myList = jsonDecode(result.toString());
          print(myList);





          share|improve this answer
























          • import 'dart:io'; import 'dart:convert'; void main() async { var request = await HttpClient().getUrl(Uri.parse('earthquake.usgs.gov/earthquakes/feed/v1.0/summary/…)); var response = await request.close(); // sends the request var result = new StringBuffer(); await for (var contents in response.transform(Utf8Decoder())) { result.write(contents); } List<dynamic> myList = jsonDecode(result.toString()); print(myList[0]['title']); }

            – DomDunk
            Nov 27 '18 at 12:41











          • Why does not it work?

            – DomDunk
            Nov 27 '18 at 12:42











          • I solved in this way:

            – DomDunk
            Nov 27 '18 at 18:00











          • 'code' var myList = jsonDecode(result.toString()); List quakes = myList['features']; } 'code'

            – DomDunk
            Nov 27 '18 at 18:01














          0












          0








          0







          Firstly, response.transform return multiple parts of result, you need to get all by joining them together:



            var result = new StringBuffer();
          await for (var contents in response.transform(Utf8Decoder())) {
          result.write(contents);
          }


          Secondly, you result is just a "text" (in json format), not an "object" as you expect, you need to decode it:



            List<dynamic> myList = jsonDecode(result.toString());
          print(myList);





          share|improve this answer













          Firstly, response.transform return multiple parts of result, you need to get all by joining them together:



            var result = new StringBuffer();
          await for (var contents in response.transform(Utf8Decoder())) {
          result.write(contents);
          }


          Secondly, you result is just a "text" (in json format), not an "object" as you expect, you need to decode it:



            List<dynamic> myList = jsonDecode(result.toString());
          print(myList);






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 24 '18 at 18:20









          yelliveryelliver

          2,54332152




          2,54332152













          • import 'dart:io'; import 'dart:convert'; void main() async { var request = await HttpClient().getUrl(Uri.parse('earthquake.usgs.gov/earthquakes/feed/v1.0/summary/…)); var response = await request.close(); // sends the request var result = new StringBuffer(); await for (var contents in response.transform(Utf8Decoder())) { result.write(contents); } List<dynamic> myList = jsonDecode(result.toString()); print(myList[0]['title']); }

            – DomDunk
            Nov 27 '18 at 12:41











          • Why does not it work?

            – DomDunk
            Nov 27 '18 at 12:42











          • I solved in this way:

            – DomDunk
            Nov 27 '18 at 18:00











          • 'code' var myList = jsonDecode(result.toString()); List quakes = myList['features']; } 'code'

            – DomDunk
            Nov 27 '18 at 18:01



















          • import 'dart:io'; import 'dart:convert'; void main() async { var request = await HttpClient().getUrl(Uri.parse('earthquake.usgs.gov/earthquakes/feed/v1.0/summary/…)); var response = await request.close(); // sends the request var result = new StringBuffer(); await for (var contents in response.transform(Utf8Decoder())) { result.write(contents); } List<dynamic> myList = jsonDecode(result.toString()); print(myList[0]['title']); }

            – DomDunk
            Nov 27 '18 at 12:41











          • Why does not it work?

            – DomDunk
            Nov 27 '18 at 12:42











          • I solved in this way:

            – DomDunk
            Nov 27 '18 at 18:00











          • 'code' var myList = jsonDecode(result.toString()); List quakes = myList['features']; } 'code'

            – DomDunk
            Nov 27 '18 at 18:01

















          import 'dart:io'; import 'dart:convert'; void main() async { var request = await HttpClient().getUrl(Uri.parse('earthquake.usgs.gov/earthquakes/feed/v1.0/summary/…)); var response = await request.close(); // sends the request var result = new StringBuffer(); await for (var contents in response.transform(Utf8Decoder())) { result.write(contents); } List<dynamic> myList = jsonDecode(result.toString()); print(myList[0]['title']); }

          – DomDunk
          Nov 27 '18 at 12:41





          import 'dart:io'; import 'dart:convert'; void main() async { var request = await HttpClient().getUrl(Uri.parse('earthquake.usgs.gov/earthquakes/feed/v1.0/summary/…)); var response = await request.close(); // sends the request var result = new StringBuffer(); await for (var contents in response.transform(Utf8Decoder())) { result.write(contents); } List<dynamic> myList = jsonDecode(result.toString()); print(myList[0]['title']); }

          – DomDunk
          Nov 27 '18 at 12:41













          Why does not it work?

          – DomDunk
          Nov 27 '18 at 12:42





          Why does not it work?

          – DomDunk
          Nov 27 '18 at 12:42













          I solved in this way:

          – DomDunk
          Nov 27 '18 at 18:00





          I solved in this way:

          – DomDunk
          Nov 27 '18 at 18:00













          'code' var myList = jsonDecode(result.toString()); List quakes = myList['features']; } 'code'

          – DomDunk
          Nov 27 '18 at 18:01





          'code' var myList = jsonDecode(result.toString()); List quakes = myList['features']; } 'code'

          – DomDunk
          Nov 27 '18 at 18:01













          0














          Simply the following



          List elements = json.decode(response.body);





          share|improve this answer
























          • error: The getter 'body' isn't defined for the class 'HttpClientResponse'. (undefined_getter at [flutter_app] libmain.dart:40)

            – DomDunk
            Nov 27 '18 at 2:12
















          0














          Simply the following



          List elements = json.decode(response.body);





          share|improve this answer
























          • error: The getter 'body' isn't defined for the class 'HttpClientResponse'. (undefined_getter at [flutter_app] libmain.dart:40)

            – DomDunk
            Nov 27 '18 at 2:12














          0












          0








          0







          Simply the following



          List elements = json.decode(response.body);





          share|improve this answer













          Simply the following



          List elements = json.decode(response.body);






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 24 '18 at 18:18









          Sami KanafaniSami Kanafani

          544511




          544511













          • error: The getter 'body' isn't defined for the class 'HttpClientResponse'. (undefined_getter at [flutter_app] libmain.dart:40)

            – DomDunk
            Nov 27 '18 at 2:12



















          • error: The getter 'body' isn't defined for the class 'HttpClientResponse'. (undefined_getter at [flutter_app] libmain.dart:40)

            – DomDunk
            Nov 27 '18 at 2:12

















          error: The getter 'body' isn't defined for the class 'HttpClientResponse'. (undefined_getter at [flutter_app] libmain.dart:40)

          – DomDunk
          Nov 27 '18 at 2:12





          error: The getter 'body' isn't defined for the class 'HttpClientResponse'. (undefined_getter at [flutter_app] libmain.dart:40)

          – DomDunk
          Nov 27 '18 at 2:12


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53459989%2fhow-can-i-get-a-list-object-by-httpclientresponse%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

          Contact image not getting when fetch all contact list from iPhone by CNContact

          count number of partitions of a set with n elements into k subsets

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks