title: Flutter - create Instance of Shared Preferences in class
body: <p>I have a class called ThemeUtil</p>
  <pre class="lang-dart prettyprint-override"><code>import 'package:shared_preferences/shared_preferences.dart';
  
  class ThemeUtil {
    SharedPreferences prefs;
    bool initialized = false;
    ThemeUtil() {
      SharedPreferences.getInstance().then((val) {
        prefs = val;
        initialized = true;
      });
    }
  
    bool getBrightness() {
      if (initialized) {
        try {
          return prefs.getBool(&quot;dark&quot;) ?? false;
        } catch (error) {
          return false;
        }
      } else {
        return null;
      }
    }
  
    setBrightness(bool _dark) {
      try {
        prefs.setBool(&quot;dark&quot;, _dark);
      } catch (error) {
        return;
      }
    }
  }
  </code></pre>
  <p>but my problem is when I use it in main.dart it always returns false which results in a white theme becuase shared prefs in ThemeUtil has not been created. Does anyone know how to fix that?</p>