title: How to cancel a running Timer() when Widget disappears
body: <p>In a StatefulWidget, I implemented a <code>deactivate()</code> method, which does some cleanup.</p>
  
  <p>Actually, the StatefulWidget periodically polls a service and reloads itself with newly received data. Therefore, it uses a <code>Timer()</code>, which periodically calls the server polling callback.</p>
  
  <p>The <code>deactivate()</code> gets called just fine. But at some deactivations of the widget, the asynchronous <code>Timer()</code> still fires a last event and calls the callback - while the widget is partly deactivated.</p>
  
  <p>This is my deactivate():</p>
  
  <pre><code>  @protected
    @mustCallSuper
    void deactivate() {
  
      // allow callback of Timeer() to not execute 
      _timerCanceled = true;
  
      // Cancel the periodic Timer()
      _timer.cancel();
  
      // Should be called LAST or FIRST?
      super.deactivate();
  
    }
  </code></pre>
  
  <p><strong>Q:General question: How to cancel some asynchronous <code>Timer()</code> and ensure, that it doesn't any longer calls the provided callback.</strong></p>
  
  <p>Or: How to write code that cancels a <code>Timer()</code> and execute code after a complete cancel operation, so that the callback of the <code>Timer()</code> definitely won't get called.</p>