title: Why Flutter GlobalKey's currentState is NULL when accessed from other file
body: <p>Here's my code:</p>
  
  <pre><code>import 'package:flutter/material.dart';
  
  void main() {
    runApp(new MyStatefulApp(key: App.appStateKey));
  }
  
  /// Part [A]. No difference when appStateKey is defined as variable.
  class App {
    static final GlobalKey&lt;MyAppState&gt; appStateKey = new GlobalKey&lt;MyAppState&gt;();
  }
  
  /// Part [B] 
  class MyStatefulApp extends StatefulWidget {
    MyStatefulApp({Key key}) :super(key: key);
  
    @override
    MyAppState createState() =&gt; new MyAppState();
  }
  
  class MyAppState extends State&lt;MyStatefulApp&gt; {
  
    int _counter = 0;
  
    add() {
      setState(() {
        _counter++;
      });
    }
  
    @override
    Widget build(BuildContext context) {
      return new MaterialApp(
        title: "App",
        theme: new ThemeData(
          primarySwatch: _counter % 2 == 0 ? Colors.blue : Colors.red,
        ),
        home: new MyHomePage(),
      );
    }
  }
  
  /// Part [C] 
  class MyHomePage extends StatefulWidget {
    MyHomePage({Key key}) : super(key: key);
  
    @override
    _MyHomePageState createState() =&gt; new _MyHomePageState();
  }
  
  class _MyHomePageState extends State&lt;MyHomePage&gt; {
  
    @override
    Widget build(BuildContext context) {
      return new Scaffold(
        appBar: new AppBar(title: new Text("Main"),      ),
        body: new FlutterLogo(),
        floatingActionButton: new FloatingActionButton(
          onPressed: () {
            App.appStateKey.currentState.add(); // (X)
          },
          tooltip: "Trigger color change",
          child: new Icon(Icons.add),
        ),
      );
    }
  }
  </code></pre>
  
  <p>In the code above, when the FAB is clicked, <code>MaterialApp</code> should rebuild, and the primary color will switch between blue and red.</p>
  
  <p>In fact, the code worked, until I attempted to split the portions of the code to different files. <code>App.appStateKey.currentState</code> on line (X) will be become <code>null</code> when:</p>
  
  <ul>
  <li>Part A (The <code>App</code> class, or the variable) is moved to another file;</li>
  <li>Part C (<code>MyHomePage</code> and <code>_MyHomePageState</code>) is moved to another file;</li>
  <li>Part A and C are moved to another file</li>
  </ul>
  
  <p>So it looks like the <code>GlobalKey.currentState</code> only work when everything involving this GlobalKey is in the same file.</p>
  
  <p>The doc only states that <code>currentState</code> will be null when <code>(1) there is no widget in the tree that matches this global key, (2) that widget is not a StatefulWidget, or the associated State object is not a subtype of T.</code> It doesn't state that everything has to be in the same file.</p>
  
  <p>Breaking classes into files may not be "the Dart way", but I assume it should work anyhow (they're all public). So this puzzles me, and I suspect if I have stumbled upon certain Flutter feature that I am not aware of. Thanks.</p>