title: Accessing Flutter context when creating StatefulWidget
body: <p>I'm having trouble accessing a services object when initializing a stateful widget. The problem comes from the <code>context</code> object not being available in <code>initState</code>.</p>
  
  <p>I'm using <code>InheritedWidget</code> to inject a services object in my <code>main.dart</code> file like so</p>
  
  <pre><code>void main() async {
    final sqflite.Database database = await _openDatabase('db.sqlite3');
    runApp(
      Services(
        database: database,
        child: MyApp(),
      ),
    );
  }
  </code></pre>
  
  <p>The <code>Services</code> object is quite straightforward. It will have more than just the <code>database</code> as a member. The idea is that the widgets don't need to know if a local database, local cache, or remote server is being accessed.</p>
  
  <pre><code>class Services extends InheritedWidget {
    final Database database;
  
    const Services({
      Key key,
      @required Widget child,
      @required this.database,
    })  : assert(child != null),
          assert(database != null),
          super(key: key, child: child);
  
    Future&lt;List&lt;models.Animal&gt;&gt; readAnimals() async {
      return db.readAnimals(database: this.database);
    }
  
    @override
    bool updateShouldNotify(InheritedWidget oldWidget) {
      return false;
    }
  
    static Services of(BuildContext context) {
      return context.inheritFromWidgetOfExactType(Services) as Services;
    }
  }
  </code></pre>
  
  <p>The trouble comes in my <code>_HomePageState</code> state when I want to load all the animals from the database. I need to access the <code>Services</code> object. I cannot access the <code>Services</code> object in <code>initState</code> so I am using <code>didChangeDependencies</code>. A problem comes when the home page is removed from the stack. It seems <code>didChangeDependences</code> is called and the access to the context object is illegal. So I created an <code>_initialized</code> flag that I can use in <code>didChangeDependencies</code> to ensure I only load the animals the first time. <strong>This seems very inelegant. Is there a better way?</strong></p>
  
  <pre><code>class _HomePageState extends State&lt;HomePage&gt; {
    bool _initialized = false;
    bool _loading = false;
    List&lt;Animal&gt; _animals;
  
    @override
    Widget build(final BuildContext context) {
      return Scaffold(
        appBar: AppBar(
          title: Text(Strings.of(this.context).appName),
        ),
        body: _HomeBody(
          loading: this._loading,
          animals: this._animals,
        ),
      );
    }
  
    @override
    void didChangeDependencies() {
      super.didChangeDependencies();
      if (!this._initialized) {
        this._initialized = true;
        this._loadAnimals();
      }
    }
  
    void _loadAnimals() async {
      this.setState(() {
        this._loading = true;
        this._animals = null;
      });
  
      final List&lt;Animal&gt; animals = await Services.of(this.context).readAnimals();
  
      this.setState(() {
        this._loading = false;
        this._animals = animals;
      });
    }
  }
  </code></pre>