title: Widget removed from tree after pushing route in Flutter
body: <p>I've come across some unexpected behaviour when pushing and popping a route that the Flutter docs haven't been able to explain.</p>
  <p>Specifically, when pushing a page route (which is then subsequently popped) from within the results of a <code>SearchDelegate</code> that is added to the widget tree via the <code>showSearch()</code> function, the widget that is used to display the results is removed from the widget tree and enters an unmounted state.</p>
  <p>Consequently, the <code>BuildContext</code> that belongs to the widget doing the routing is disposed of, and thus any use of <code>context</code> in the build method causes an error. I've provided a sample Flutter program below that illustrates this issue (it's a bit lengthy so bear with me):</p>
  <pre><code>import 'package:flutter/material.dart';
  
  void main() =&gt; runApp(MyApp());
  
  class MyApp extends StatelessWidget {
    // This widget is the root of your application.
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        home: Scaffold(
          body: Center(
            child: Builder(
              builder: (BuildContext context) {
                return TextButton(
                  child: Text(&quot;Search&quot;),
                  onPressed: () {
                    showSearch(context: context, delegate: Search());
                  }
                );
              }
            )
          )
        )
      );
    }
  }
  
  class Search extends SearchDelegate {
    @override
    Widget buildResults(BuildContext context) =&gt; SearchResults();
    // Irrelevant
    @override
    List&lt;Widget&gt; buildActions(BuildContext context) =&gt; null;
    @override
    Widget buildLeading(BuildContext context) =&gt; null;
    @override
    Widget buildSuggestions(BuildContext context) =&gt; Text(&quot;Suggestions&quot;);
  }
  
  class SearchResults extends StatefulWidget {
    @override
    SearchResultsState createState() =&gt; SearchResultsState();
  }
  
  class SearchResultsState extends State&lt;SearchResults&gt; {
    @override
    Widget build(BuildContext context) {
      return TextButton(
        child: Text(&quot;Push&quot;),
        onPressed: () async {
          await Navigator.push&lt;String&gt;(
            context,
            MaterialPageRoute&lt;String&gt;(
              builder: (BuildContext context) =&gt; RouteA()
            )
          );
          // ============================= ERROR =============================
          print(mounted); // This should print true (spoiler: it prints false)!
          // =================================================================
        }
      );
    }
  }
  
  class RouteA extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        body: Center(child:
          TextButton(
            child: Text(&quot;Pop&quot;),
            onPressed: () {
              Navigator.of(context).pop();
            }
          )
        )
      );
    }
  }
  </code></pre>
  <p>Please note that this is a simplified version of the program in which I originally witnessed this issue. I'm aware that the widget hierarchy I've presented here may seem a bit nonsensical; however, I'm more interested in why this is happening in the first place for this specific configuration.</p>
  <p>Any explanations and appropriate workarounds would be greatly appreciated!</p>