title: Flutter - How the NavigatorObserver class works?
body: <p>I'm trying to detect when the <strong>navigator</strong> performs the basic operations (<strong>push</strong> and <strong>pop</strong>), I found that the class to achieve this is with the <a href="https://docs.flutter.io/flutter/widgets/NavigatorObserver-class.html" rel="noreferrer">NavigatorObserver</a> class, but I can't find an example on how it works.</p>
  
  <p>I already tried implements the interface:</p>
  
  <pre><code>class MyClassState extends State&lt;MyClass&gt; with NavigatorObserver{
  
      ....
  
      @override
      void didPop(Route&lt;dynamic&gt; route, Route&lt;dynamic&gt; previousRoute) {
          print('This is never get called');
      }
  
      @override
      void didPush(Route&lt;dynamic&gt; route, Route&lt;dynamic&gt; previousRoute) {
          print('This is never get called');
      }
  }
  </code></pre>
  
  <p>Of course I'm calling <code>Navigator.of(context).push(...);</code>
  But the override methods, never get called.</p>
  
  <p>I suspect the binding with the navigator is missing, but I'm not sure.</p>
  
  <p>Any suggestions?</p>
  
  <p>Thanks in advance!</p>
  
  <p><strong>EDIT:</strong> </p>
  
  <p>I think what I want to do is very simple but I can't figure out how to do it.</p>
  
  <p>I have a <strong>page A</strong> that pushes <strong>page B</strong>.</p>
  
  <p>On <strong>page B</strong> I have a <strong>periodic timer</strong> that <strong>must</strong> be on there (on <strong>page B</strong>).</p>
  
  <p>All what I want to do is to cancel the timer with <code>timer.cancel()</code> when the <strong>page B</strong> get popped out (with back button on Android or back button in appbar), since when the <strong>page B</strong> get popped out the timer still executing even if the <strong>page B</strong> has gone.</p>
  
  <ul>
  <li>I can't pass the timer as param with the Navigator.pop() since I don't handle the back buttons (the one in the appbar and the Android back button).</li>
  </ul>
  
  <p><strong>EDIT 2:</strong></p>
  
  <p>-I found a way that solves the problem, what I did was cancel the timer on the </p>
  
  <pre><code>@override
  void dispose() {
    timer.cancel();
    super.dispose();
  }
  </code></pre>
  
  <p>method. But I'm not sure if this is the best way to accomplish it.</p>
  
  <p><strong>NOTE:</strong> </p>
  
  <p>With this workaround the timer still executing if I press the Home button or open the multitask on Android, and when go back to the app this are restarted but again the timer still executing its code.</p>