title: Component unmount in flutter (equivalent of React componentWillUnmount) - Flutter web
body: <p>I have widget that is unmounted on page change.</p>
  <p>I am using flutter web.</p>
  <p>I looked at all the methods <a href="https://stackoverflow.com/a/62946789/3808307">here</a> but I can't seem to find the one.</p>
  <p>I tried</p>
  <pre><code>@override
  void dispose() {
    print(&quot;dispose?&quot;); 
    super.dispose();   
  }
  
  @override
  void deactivate() {
    print(&quot;deactivate&quot;);
    super.deactivate();
  }
  </code></pre>
  <p>and neither is called.</p>
  <p>It is a very simple thing I need to detect when the widget is not being &quot;built&quot; (rendered). I don't think having something to dtect the page change is the solution, it seems overkill.
  Thank you</p>
  <p>Adding code sample</p>
  <pre><code>import 'package:flutter/material.dart';
  
  void main() {
    runApp(MaterialApp(
      title: 'Named Routes Demo',
      initialRoute: '/',
      routes: {
        '/': (context) =&gt; FirstScreen(),
        '/second': (context) =&gt; SecondScreen(),
      },
    ));
  }
  
  class FirstScreen extends StatefulWidget {
    FirstScreen({Key key}) : super(key: key);
  
    @override
    _FirstScreenState createState() =&gt; _FirstScreenState();
  }
  
  class _FirstScreenState extends State&lt;FirstScreen&gt; {
    @override
  void dispose() {
    print(&quot;dispose&quot;); 
    super.dispose();   
  }
  
  @override
  void deactivate() {
    print(&quot;deactivate&quot;);
    super.deactivate();
  }
    
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(
          title: Text('First Screen'),
        ),
        body: Center(
          child: ElevatedButton(
            child: Text('Launch screen'),
            onPressed: () {
              Navigator.pushNamed(context, '/second');
            },
          ),
        ),
      );
    }
  }
  
  class SecondScreen extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(
          title: Text(&quot;Second Screen&quot;),
        ),
        body: Center(
          child: ElevatedButton(
            onPressed: () {
              // Navigate back to the first screen by popping the current route
              // off the stack.
              Navigator.pop(context);
            },
            child: Text('Go back!'),
          ),
        ),
      );
    }
  }
  </code></pre>
  <p>It seems neither deactive nor dispose are called when navigating to the second screen and the first screen widget stops being part of the render tree. So what to call to detect that Screen 1 leaves the render tree?</p>