Difference between .then() and .whenCompleted() methods when working with Futures?GWT vs Dart - what are the main differences? Is Dart a potential replacement of GWT?Completer and Future in dart?Launching multiple async futures in response to eventsDart - running a project does not load the dart file and not printing in the consoleDifference between a Future returning another Future and NotUsing Dart in Chrome extension content script does not run?Future execution leads to unhandled exceptionDart AppEngine Headers already sent when using FuturesCan I call any dart function inside aqueduct Response controller?How to Determine in Dart when a method is Overridden?

Defense against attacks using dictionaries

Are illustrations in novels frowned upon?

Why don't we use Cavea-B

IndexOptimize - Configuration

Why is 日本 read as "nihon" but not "nitsuhon"?

How to draw a square on cylinder?

Sleeping solo in a double sleeping bag

Factoring the square of this polynomial?

Is there a known non-euclidean geometry where two concentric circles of different radii can intersect? (as in the novel "The Universe Between")

LeetCode: Pascal's Triangle C#

Co-author responds to email by mistake cc'ing the EiC

Why would the US President need briefings on UFOs?

Is there such a thing as too inconvenient?

System to validate run time complexity requirements

How to compare two different formulations of a problem?

Shouldn't the "credit score" prevent Americans from going deeper and deeper into personal debt?

How to persuade recruiters to send me the Job Description?

Was Switzerland really impossible to invade during WW2?

Don't understand MOSFET as amplifier

Were there 486SX revisions without an FPU on the die?

When translating the law, who ensures that the wording does not change the meaning of the law?

Do ability scores have any effect on casting Wish spell

In an emergency, how do I find and share my position?

Fancy String Replace



Difference between .then() and .whenCompleted() methods when working with Futures?


GWT vs Dart - what are the main differences? Is Dart a potential replacement of GWT?Completer and Future in dart?Launching multiple async futures in response to eventsDart - running a project does not load the dart file and not printing in the consoleDifference between a Future returning another Future and NotUsing Dart in Chrome extension content script does not run?Future execution leads to unhandled exceptionDart AppEngine Headers already sent when using FuturesCan I call any dart function inside aqueduct Response controller?How to Determine in Dart when a method is Overridden?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















I'm exploring Futures in Dart, and I'm confused about these two methods that Future offers. Whats the main difference between them?



Lets say I want to read a .txt using .readAsString(), I would do it like this:



void main()
File file = new File('text.txt');
Future content = file.readAsString();
content.then((data)
print(content);
);



So .then() is like a callback that fires a function once Future is completed.



But I see there is also .whenComplete() that can also fire an function once Future completes. Something like this :



void main()
File file = new File('text.txt');
Future content = file.readAsString();
content.whenComplete(()
print("Completed");
);



Difference I see here is that .then() has access to data that was returned!
What is .whenCompleted() used for? When to chose one over the other?



And also on these two links, .then() and .whenCompleted() at the end of a page there are implementations:



.then():



Future<R> then<R>(FutureOr<R> onValue(T value), Function onError);


.whenCompleted():



Future<T> whenComplete(FutureOr action());


What does Future<R> means? Or Future<T>? I get that Future is a type, but what are R and T?



Thanks!










share|improve this question






























    2















    I'm exploring Futures in Dart, and I'm confused about these two methods that Future offers. Whats the main difference between them?



    Lets say I want to read a .txt using .readAsString(), I would do it like this:



    void main()
    File file = new File('text.txt');
    Future content = file.readAsString();
    content.then((data)
    print(content);
    );



    So .then() is like a callback that fires a function once Future is completed.



    But I see there is also .whenComplete() that can also fire an function once Future completes. Something like this :



    void main()
    File file = new File('text.txt');
    Future content = file.readAsString();
    content.whenComplete(()
    print("Completed");
    );



    Difference I see here is that .then() has access to data that was returned!
    What is .whenCompleted() used for? When to chose one over the other?



    And also on these two links, .then() and .whenCompleted() at the end of a page there are implementations:



    .then():



    Future<R> then<R>(FutureOr<R> onValue(T value), Function onError);


    .whenCompleted():



    Future<T> whenComplete(FutureOr action());


    What does Future<R> means? Or Future<T>? I get that Future is a type, but what are R and T?



    Thanks!










    share|improve this question


























      2












      2








      2








      I'm exploring Futures in Dart, and I'm confused about these two methods that Future offers. Whats the main difference between them?



      Lets say I want to read a .txt using .readAsString(), I would do it like this:



      void main()
      File file = new File('text.txt');
      Future content = file.readAsString();
      content.then((data)
      print(content);
      );



      So .then() is like a callback that fires a function once Future is completed.



      But I see there is also .whenComplete() that can also fire an function once Future completes. Something like this :



      void main()
      File file = new File('text.txt');
      Future content = file.readAsString();
      content.whenComplete(()
      print("Completed");
      );



      Difference I see here is that .then() has access to data that was returned!
      What is .whenCompleted() used for? When to chose one over the other?



      And also on these two links, .then() and .whenCompleted() at the end of a page there are implementations:



      .then():



      Future<R> then<R>(FutureOr<R> onValue(T value), Function onError);


      .whenCompleted():



      Future<T> whenComplete(FutureOr action());


      What does Future<R> means? Or Future<T>? I get that Future is a type, but what are R and T?



      Thanks!










      share|improve this question














      I'm exploring Futures in Dart, and I'm confused about these two methods that Future offers. Whats the main difference between them?



      Lets say I want to read a .txt using .readAsString(), I would do it like this:



      void main()
      File file = new File('text.txt');
      Future content = file.readAsString();
      content.then((data)
      print(content);
      );



      So .then() is like a callback that fires a function once Future is completed.



      But I see there is also .whenComplete() that can also fire an function once Future completes. Something like this :



      void main()
      File file = new File('text.txt');
      Future content = file.readAsString();
      content.whenComplete(()
      print("Completed");
      );



      Difference I see here is that .then() has access to data that was returned!
      What is .whenCompleted() used for? When to chose one over the other?



      And also on these two links, .then() and .whenCompleted() at the end of a page there are implementations:



      .then():



      Future<R> then<R>(FutureOr<R> onValue(T value), Function onError);


      .whenCompleted():



      Future<T> whenComplete(FutureOr action());


      What does Future<R> means? Or Future<T>? I get that Future is a type, but what are R and T?



      Thanks!







      dart






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 27 at 15:43









      MatijaMatija

      1,0065 silver badges12 bronze badges




      1,0065 silver badges12 bronze badges

























          1 Answer
          1






          active

          oldest

          votes


















          6













          .whenComplete will fire a function either when the Future completes with an error or not, instead .then will fire a function after the Future completes without an error.



          Quote from the .whenComplete API DOC




          This is the asynchronous equivalent of a "finally" block.







          share|improve this answer

























          • Thanks Mattia, and what type is <R> and <T>?

            – Matija
            Mar 27 at 17:50






          • 1





            They are not actual types, but generics.

            – Mattia
            Mar 27 at 18:05












          • Thank you, this helped me a lot!

            – Matija
            Mar 27 at 18:58










          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%2f55381236%2fdifference-between-then-and-whencompleted-methods-when-working-with-future%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          6













          .whenComplete will fire a function either when the Future completes with an error or not, instead .then will fire a function after the Future completes without an error.



          Quote from the .whenComplete API DOC




          This is the asynchronous equivalent of a "finally" block.







          share|improve this answer

























          • Thanks Mattia, and what type is <R> and <T>?

            – Matija
            Mar 27 at 17:50






          • 1





            They are not actual types, but generics.

            – Mattia
            Mar 27 at 18:05












          • Thank you, this helped me a lot!

            – Matija
            Mar 27 at 18:58















          6













          .whenComplete will fire a function either when the Future completes with an error or not, instead .then will fire a function after the Future completes without an error.



          Quote from the .whenComplete API DOC




          This is the asynchronous equivalent of a "finally" block.







          share|improve this answer

























          • Thanks Mattia, and what type is <R> and <T>?

            – Matija
            Mar 27 at 17:50






          • 1





            They are not actual types, but generics.

            – Mattia
            Mar 27 at 18:05












          • Thank you, this helped me a lot!

            – Matija
            Mar 27 at 18:58













          6












          6








          6







          .whenComplete will fire a function either when the Future completes with an error or not, instead .then will fire a function after the Future completes without an error.



          Quote from the .whenComplete API DOC




          This is the asynchronous equivalent of a "finally" block.







          share|improve this answer













          .whenComplete will fire a function either when the Future completes with an error or not, instead .then will fire a function after the Future completes without an error.



          Quote from the .whenComplete API DOC




          This is the asynchronous equivalent of a "finally" block.








          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 27 at 16:40









          MattiaMattia

          9641 gold badge6 silver badges17 bronze badges




          9641 gold badge6 silver badges17 bronze badges















          • Thanks Mattia, and what type is <R> and <T>?

            – Matija
            Mar 27 at 17:50






          • 1





            They are not actual types, but generics.

            – Mattia
            Mar 27 at 18:05












          • Thank you, this helped me a lot!

            – Matija
            Mar 27 at 18:58

















          • Thanks Mattia, and what type is <R> and <T>?

            – Matija
            Mar 27 at 17:50






          • 1





            They are not actual types, but generics.

            – Mattia
            Mar 27 at 18:05












          • Thank you, this helped me a lot!

            – Matija
            Mar 27 at 18:58
















          Thanks Mattia, and what type is <R> and <T>?

          – Matija
          Mar 27 at 17:50





          Thanks Mattia, and what type is <R> and <T>?

          – Matija
          Mar 27 at 17:50




          1




          1





          They are not actual types, but generics.

          – Mattia
          Mar 27 at 18:05






          They are not actual types, but generics.

          – Mattia
          Mar 27 at 18:05














          Thank you, this helped me a lot!

          – Matija
          Mar 27 at 18:58





          Thank you, this helped me a lot!

          – Matija
          Mar 27 at 18:58








          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















          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%2f55381236%2fdifference-between-then-and-whencompleted-methods-when-working-with-future%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

          How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

          155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

          해남서초등학교 학교 연혁 학교 동문 외부 링크 둘러보기 메뉴북위 34° 34′ 21″ 동경 126° 35′ 50″ / 북위 34.5726385° 동경 126.5972670°  / 34.5726385; 126.5972670해남서초등학교eh