title: Cancel execution of method after it starts in Flutter
body: <p>Consider this method:</p>
  <pre><code>Future&lt;void&gt; methodWithAlotOfSteps()async{
    callMethodA();
    await callMethodB();
    ...
    return ...;
  }
  </code></pre>
  <p>which makes some computation. Suppose I want the user to be able to stop this process at any point in time (when he taps <code>cancel</code> button for example).</p>
  <p>How can I stop the execution of the above method no matter what line in the method the &quot;program counter&quot; has reached when the user presses <code>cancel</code>.</p>
  <p>I am looking for something like <code>methodWithAlotOfSteps.cancel();</code>.</p>
  <p>I tried using <code>CancelableCompleter</code>, but even though the <code>Future</code> is cancelled and <code>onCancel</code> method of the completer is called, but the function continues execution.</p>
  <p>I know I can set a boolean flag and check it after each &quot;step&quot; (&quot;line&quot;, &quot;call to a method&quot;),such as :</p>
  <pre><code>Future&lt;void&gt; methodWithAlotOfSteps()async{
    if(!completer.isCancelled)
    callMethodA();
    if(!completer.isCancelled)
    await callMethodB();
    ...
    return ...;
  }
  </code></pre>
  <p>but is there a better way of doing this?</p>