title: Flutter: Setting BuildContext to null from another class
body: <p>I would like to know how to nullify a <code>BuildContext</code> from another class to prevent it from being used by an asynchronous function after context's widget has been disposed. </p>
  
  <p>I assumed that it was passed by reference, so I tried setting it to <code>null</code> in the original widget (in my use case, I was setting it to <code>null</code> in the dispose method and the <code>onWillPop</code> method, both to no avail). I created a simple example app for what I would like to do. As you can see if you run the app, the context is not set to <code>null</code>.</p>
  
  <pre><code>import 'dart:async';
  
  import 'package:flutter/material.dart';
  
  void main() =&gt; runApp(MyApp());
  
  class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        title: 'Setting BuildContext to null',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: MyHomePage(title: 'Setting BuildContext to null'),
      );
    }
  }
  
  class MyHomePage extends StatefulWidget {
    MyHomePage({Key key, this.title}) : super(key: key);
    final String title;
  
    @override
    _MyHomePageState createState() =&gt; _MyHomePageState();
  }
  
  class _MyHomePageState extends State&lt;MyHomePage&gt; {
    AsyncClass _asyncClass = AsyncClass();
    Duration duration = const Duration(seconds: 5);
    void toggleContext(BuildContext context){
      Timer.periodic(duration, (_){
        print("Setting context to null");
        context = null;
  
      });
    }
  
    @override
    Widget build(BuildContext context) {
      toggleContext(context);
      _asyncClass.asyncFunc(context);
      return Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: Center(),
      );
    }
  }
  
  class AsyncClass{
    void asyncFunc(BuildContext context){
      const duration = const Duration(seconds: 1);
      Timer.periodic(duration, (_){
        if(context==null){
          print("Context is null");
        } else {
          print("Context is not null");
        }
  
      });
    }
  }
  </code></pre>