-2

I have a class called ThemeUtil

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("dark") ?? false;
      } catch (error) {
        return false;
      }
    } else {
      return null;
    }
  }

  setBrightness(bool _dark) {
    try {
      prefs.setBool("dark", _dark);
    } catch (error) {
      return;
    }
  }
}

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?

TuiTui182
  • 1
  • 3

1 Answers1

1

Problem you are facing is called accessing class instance. Each time you are accessing your utils class, you are creating another class instance of that class, where in the constructor of it, you constantly generate asynchronously new SharedPreferences instance. That's why you are always getting false values. What I would propose is to create singleton Utils class, where you always access already assigned prefs.

class LocalStorage {
  static final LocalStorage _instance = LocalStorage._privateConstructor();
  factory LocalStorage() {
    return _instance;
  }
  SharedPreferences _prefs;

  LocalStorage._privateConstructor() {
    SharedPreferences.getInstance().then((prefs) {
      _prefs = prefs;
    });
  }
}

And now make sure you initialise this singleton at app launch:

void main() {
  LocalStorage();
  runApp(MyApp());
}

After initial creation of SharedPreferences object, you can synchronously access it's content anywhere in the app by calling:

LocalStorage().getBrightness();
Aleksandar
  • 1,457
  • 13
  • 30
  • I did what you said and then used _dark = LocalStorage().prefs.getBool("dark"); but it said NoSuchMethodError: the method 'getBool' was called on null. Receiver: null – TuiTui182 Jul 31 '20 at 19:16
  • Nooo, you should never access _localStorage itself. You need to invoke metnod bool isDarkModeOn() which will take care of fetching bool from local storage. That way you keep your string tags used for storage private to this single file. – Aleksandar Jul 31 '20 at 19:33
  • So is it right to just create a file called LocalStrorage.dart then call it before I start my app and then in my app just use ThemeUtil as I would normally do? Because LocalStorage().getBrightness(); kind of confuses me. – TuiTui182 Aug 03 '20 at 15:48
  • And I just discoverd by just running LocalStorage before MyApp it seems like I cant store anything anymore – TuiTui182 Aug 03 '20 at 17:03
  • If you call the storage like in the answer above, it will work as expected. And when you need to store/read values, you call it as LocalStorage().brigthness when you create getter. Or call LocalStorage().setBrightness(brightness); This is normal way of using singleton objects and it's methods... – Aleksandar Aug 04 '20 at 16:50