title: Flutter: Shared Preferences null on Startup
body: <p>Problem: Shared preference bool value is <code>null</code> on startup even though I have given it a value if <code>prefs.getBool('myBool')</code> returns <code>null</code> (though my shared preferences value should already be set and saved).  It does, however, work by the time I press a button (I assume because it has finished running the async code).</p>
  
  <p>Question: How can I force shared preferences to load on startup (so my value is not <code>null</code>) without having to press the print button?</p>
  
  <p>Example Code:</p>
  
  <pre><code>import 'package:flutter/material.dart';
  import 'package:flutter/widgets.dart';
  import 'package:shared_preferences/shared_preferences.dart';
  
  void main() =&gt; runApp(new MyApp());
  
  class MyApp extends StatefulWidget {
    MyApp({Key key}) : super(key: key);
  
    @override
    createState() =&gt; new MyAppState();
  }
  
  class MyAppState extends State&lt;MyApp&gt; {
    final padding = const EdgeInsets.all(50.0);
  
    @override
    void initState() {
      super.initState();
  
      MySharedPreferences.load();
      MySharedPreferences.printMyBool();
    }
  
    @override
      Widget build(BuildContext context) {
        return new MaterialApp(
          home: new Scaffold(
            body: new Padding(
              padding: padding,
              child: new Column(
                children: &lt;Widget&gt;[
                  new Padding(
                    padding: padding,
                    child: new RaisedButton(
                      child: new Text('Save True'),
                      onPressed: () =&gt; MySharedPreferences.save(myBool: true),
                    ),
                  ),
                  new Padding(
                    padding: padding,
                    child: new RaisedButton(
                      child: new Text('Save False'),
                      onPressed: () =&gt; MySharedPreferences.save(myBool: false),
                    ),
                  ),
                  new Padding(
                    padding: padding,
                    child: new RaisedButton(
                      child: new Text('Print myBool'),
                      onPressed: () =&gt; MySharedPreferences.printMyBool(),
                  ),
                ),
              ],
            ),
          ), 
        ),
      );
    }
  }
  
  class MySharedPreferences {
    static bool _myBool;
  
    static void load() async {
      SharedPreferences prefs = await SharedPreferences.getInstance();
      _myBool = prefs.getBool('myBool') ?? false;
    }
  
    static void save({myBool: bool}) async {
      SharedPreferences prefs = await SharedPreferences.getInstance();
      _myBool = myBool;
      await prefs.setBool('myBool', _myBool);
    }
  
    static void printMyBool() {
      print('myBool: ${_myBool.toString()}');
    }
  }
  </code></pre>
  
  <p>Results:
    On startup, <code>myBool: null</code> is printed.  Once the button is pressed, <code>myBool: false/true</code> is then printed.</p>