title: Why do I get "Looking up a deactivated widget's ancestor is unsafe" on my PopupMenuItem?
body: <p>I have a Flutter page/widget, with a search box and a ListView populated from a <a href="https://pub.dev/packages/flutter_bloc" rel="noreferrer">flutter_bloc</a> BLOC.<br>
  When text is entered, I call a BLOC method to search for results, and the result is presented in a ListView.<br>
  <a href="https://i.stack.imgur.com/5Sga7.png" rel="noreferrer"><img src="https://i.stack.imgur.com/5Sga7.png" alt="enter image description here"></a>
  When I click on the <code>PopupMenuItem</code> inside the <code>ListTile</code> item I get:</p>
  
  <p><a href="https://i.stack.imgur.com/svOue.png" rel="noreferrer"><img src="https://i.stack.imgur.com/svOue.png" alt="enter image description here"></a></p>
  
  <blockquote>
    <p>Looking up a deactivated widget's ancestor is unsafe. At this point
    the state of the widget's element tree is no longer stable. To safely
    refer to a widget's ancestor in its dispose() method, save a reference
    to the ancestor by calling dependOnInheritedWidgetOfExactTpye() in the
    widget's didChangeDepencies() method.</p>
  </blockquote>
  
  <p>If I replace the BLOC "dance" with a simple static list initialization of the <code>stations</code> variable, it works fine.</p>
  
  <p>I am completely lost on what to do?
  Can someone please give me some pointers?</p>
  
  <p><strong>Edit</strong>:
  Here's a repro sample (Android only):
  <a href="https://github.com/MagnusJohansson/bloc_test" rel="noreferrer">https://github.com/MagnusJohansson/bloc_test</a><br>
  Click on the search icon in the app, enter "test" as the search term. Click on the 3-dot menu to trigger the error (might have to click twice).
  If not doing the sqlite query, and instead returning a static list (as seen in the comment in file /bloc/searchpresets/search_presets_bloc.dart line 55) it works fine.</p>
  
  <pre><code>class SearchPresetsPage extends StatefulWidget {
    @override
    State&lt;StatefulWidget&gt; createState() =&gt; _SearchPresetsPageState();
  }
  
  final GlobalKey&lt;RefreshIndicatorState&gt; _refreshIndicatorKey2 =
      new GlobalKey&lt;RefreshIndicatorState&gt;();
  
  final GlobalKey&lt;ScaffoldState&gt; scaffoldKey2 = new GlobalKey&lt;ScaffoldState&gt;();
  
  final snackBar = SnackBar(
    content: Text('Added as a favorite'),
    action: SnackBarAction(
      label: 'Undo',
      onPressed: () {},
    ),
  );
  
  class _SearchPresetsPageState extends State&lt;SearchPresetsPage&gt; {
    final TextEditingController _filter = new TextEditingController();
    List&lt;Station&gt; stations = [];
  
    _textInputSearch() {
      if (_filter.text.length &gt;= 2) {
        BlocProvider.of&lt;SearchPresetsBloc&gt;(context)
            .add(SearchPresets(_filter.text));
      }
    }
  
    @override
    void initState() {
      _filter.addListener(_textInputSearch);
      super.initState();
    }
  
    Widget buildInitialStationsInput(BuildContext context, String message) {
      return Column(children: &lt;Widget&gt;[
        Center(child: Text(message)),
      ]);
    }
  
    Widget buildLoading() {
      return Center(
        child: LinearProgressIndicator(
          value: null,
        ),
      );
    }
  
    Widget _buildListView() {
      return RefreshIndicator(
        key: _refreshIndicatorKey2,
        child: Column(
          children: &lt;Widget&gt;[
            Container(
              color: Colors.grey[300],
              alignment: Alignment.topLeft,
              child: Padding(
                padding: const EdgeInsets.all(4.0),
                child: Text(
                  "${stations.length} Stations:",
                ),
              ),
            ),
            Expanded(
              child: ListView.builder(
                itemCount: stations != null ? stations.length : 0,
                itemBuilder: (context, index) {
                  return Card(
                    child: ListTile(
                      dense: true,
                      title: Text(stations[index].name),
                      leading: IconButton(
                        alignment: Alignment.center,
                        icon: Icon(Icons.play_arrow),
                        onPressed: () async {
                        },
                      ),
                      trailing: PopupMenuButton(
                        onSelected: (value) async {
                        },
                        itemBuilder: (context) {
                          return [
                            PopupMenuItem(
                              value: 1,
                              child: Text("Add to favorites"),
                            ),
                          ];
                        },
                      ),
                    ),
                  );
                },
              ),
            ),
          ],
        ),
        onRefresh: () async {
          BlocProvider.of&lt;SearchPresetsBloc&gt;(context)
              .add(SearchPresets(_filter.text));
        },
      );
    }
  
    @override
    Widget build(BuildContext context) {
      return BlocListener&lt;SearchPresetsBloc, SearchPresetsState&gt;(
        listener: (BuildContext context, SearchPresetsState state) {},
        child: Scaffold(
          key: scaffoldKey2,
          appBar: AppBar(
            actions: &lt;Widget&gt;[],
            title: Container(
              color: Colors.white,
              child: TextField(
                controller: _filter,
                decoration: new InputDecoration(
                    filled: true,
                    suffixIcon: IconButton(
                      icon: Icon(Icons.clear),
                      onPressed: () {
                        _filter.clear();
                        setState(() {
                          stations = [];
                          BlocProvider.of&lt;SearchPresetsBloc&gt;(context)
                              .add(ClearSearch());
                        });
                      },
                    ),
                    prefixIcon: new Icon(Icons.search),
                    hintText: 'Search...'),
              ),
            ),
          ),
          body: BlocBuilder&lt;SearchPresetsBloc, SearchPresetsState&gt;(
              builder: (BuildContext context, SearchPresetsState state) {
            if (state is InitialSearchPresetsState) {
              return buildInitialStationsInput(context,
                  "Search for stations in the search box above (at least 2 characters)");
            }
            if (state is SearchPresetsLoading) {
              return buildLoading();
            }
            if (state is SearchPresetsLoaded) {
              stations = state.stations;
              return _buildListView();
            }
            if (state is SearchPresetsError) {
              return buildInitialStationsInput(
                  context, state.exception.toString());
            }
            return Container();
          }),
        ),
      );
    }
  }
  </code></pre>