title: Flutter : Cancel Timer In Dispose Not Working
body: <p>I have create running clock using timer with this code : </p>
  
  <h3>Source Code</h3>
  
  <pre><code>
  class LiveClock extends StatefulWidget {
    @override
    _LiveClockState createState() =&gt; _LiveClockState();
  }
  
  class _LiveClockState extends State&lt;LiveClock&gt; {
    String _timeString;
    String _dateString;
  
    Timer _timerClock;
  
    String _formatTime(DateTime dateTime) =&gt; DateFormat.Hms().format(dateTime);
    String _formatDate(DateTime dateTime) =&gt;
        DateFormat.yMMMMEEEEd(appConfig.indonesiaLocale).format(dateTime);
  
    @override
    void initState() {
      super.initState();
      _timeString = _formatTime(DateTime.now());
      _dateString = _formatDate(DateTime.now());
      _timerClock = Timer.periodic(Duration(seconds: 1), _getTime);
    }
  
    @override
    void dispose() {
      _timerClock.cancel();
      super.dispose();
    }
  
    void _getTime(Timer timer) {
      final DateTime now = DateTime.now();
      final String formattedTime = _formatTime(now);
      setState(() =&gt; _timeString = formattedTime);
    }
  
    @override
    Widget build(BuildContext context) {
      print('This Rebuild');
  
      return Text(
        '$_dateString $_timeString ',
        textAlign: TextAlign.center,
      );
    }
  }
  
  </code></pre>
  
  <h3>Result</h3>
  
  <p><img src="https://i.stack.imgur.com/Av99x.jpg" height="500"> </p>
  
  <blockquote>
    <p>But the problem is , if i navigate to another screen , the timer still running although i have dispose the timer. </p>
  </blockquote>
  
  <p><a href="https://i.stack.imgur.com/wAwlg.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wAwlg.gif" alt="enter image description here"></a></p>
  
  <p><p> did I make mistake or it's behaviour the timer ? </p>