title: Flutter: Shared preferences singleton not initializing properly
body: <p>I'm very new to Flutter and Dart, comming from android, bringing some of my habbits with me, I want to implement a SharedPreferences singleton object to simplify and avoid repetition (duplication).
  this is my SharedPreferences singleton class:</p>
  
  <pre><code>import 'package:shared_preferences/shared_preferences.dart';
  import 'package:synchronized/synchronized.dart';
  
  class MySharedPreferences {
    static MySharedPreferences _instance;
    SharedPreferences _preferences;
  
    // keys
    final String _logged = "LOGGED";
    final String _accessToken = "ACCESS_TOKEN";
  
    MySharedPreferences._() {
      _initSharedPreferences();
    }
  
    static MySharedPreferences getInstance() {
      var lock = new Lock();
      if (_instance == null) {
        lock.synchronized(() =&gt; {_instance = new MySharedPreferences._()});
        return _instance;
      } else
        return _instance;
    }
  
    _initSharedPreferences() async {
      _preferences = await SharedPreferences.getInstance();
    }
  
    bool checkLogged() {
      return _preferences.getBool(_logged);
    }
  
    void setLogged(bool logged) {
      _preferences.setBool(_logged, logged);
    }
  </code></pre>
  
  <p>well most of this logic is what i used to do in android, and used to work perfectly, but when i tried testing it, the singleton is always null, here is the test:</p>
  
  <pre><code>import 'package:flutter_test/flutter_test.dart';
  import 'package:reportingsystem/local/my_shared_preferences.dart';
  
  void main() {
    TestWidgetsFlutterBinding.ensureInitialized();
    test('Test the shared_preferences', () {
      MySharedPreferences preferences = MySharedPreferences.getInstance();
      preferences.setLogged(true);
      expect(preferences.checkLogged(), true);
      preferences.setLogged(false);
      expect(preferences.checkLogged(), false);
    });
  }
  </code></pre>
  
  <p>The test fails because the "preferences" object is null, i don't know what wrong, and i don't find much about it in the docs.
  here is the stacktrace:</p>
  
  <pre><code>dart:core                                                       Object.noSuchMethod
  package:reportingsystem/local/my_shared_preferences.dart 34:18  MySharedPreferences.setLogged
  test\shared_preferences_test.dart 8:17                          main.&lt;fn&gt;
  test\shared_preferences_test.dart 6:39                          main.&lt;fn&gt;
  
  NoSuchMethodError: The method 'setBool' was called on null.
  Receiver: null
  Tried calling: setBool("LOGGED", true)
  </code></pre>