Search code examples
fluttersharedpreferences

Flutter: Shared Preferences taking two reloads each and every time to update a value in flutter


I am a newbie and just started coding. I am making a simple application using flutter which prints a new quote every day, I am using shared preferences for this and it is taking two Hot restarts or opening the app twice to update the value. I'm not sure why its happening, can you please throw some light. Here is my code:

  Future<int> setDay(n) async {
  final pref = await SharedPreferences.getInstance();
  pref.setInt('day', n);
}

Future<String> setQuote() async {
  SharedPreferences pref = await SharedPreferences.getInstance();
  pref.setString('quote', fromallquotes['$randomnumber']);
}

Future<String> changequote() async {
  final pref = await SharedPreferences.getInstance();
  var currentday = pref.getInt('day');
  var quote = pref.getString('quote');
  if (quote == null) {
    todaysquote = fromallquotes["$randomnumber"];
  }
  if (currentday == DateTime.now().weekday) {
    todaysquote = quote;
  } else {
    setQuote();
    todaysquote = quote;
    setDay(DateTime.now().weekday);
  }
  return todaysquote;
}

Solution

  • let me give you my code snippet so you will get the idea

    import 'package:shared_preferences/shared_preferences.dart';
    
    class SPHelper {
      static SharedPreferences prefs;
      static void setPref(SharedPreferences prefs1) {
        prefs = prefs1;
      }
    
      static int getInt(String key) {
        return prefs.getInt(key) ?? 0;
      }
    
      static void setInt(String key, int value) {
        prefs.setInt(key, value);
        //prefs.commit();
      }
    
      static void setStringList(String key, List<String> lstCart) {
        prefs.setStringList(key, lstCart);
      }
    
      static List<String> getStringList(String key) {
        return (prefs.containsKey(key)) ? prefs.getString(key) : List<String>();
      }
    
      static void removeCartList(String key) {
        var lst = List<String>();
        prefs.setStringList(key, lst);
      }
    
      static String getString(String key) {
        return prefs.getString(key) ?? "";
      }
    
      static void setString(String key, String value) {
        prefs.setString(key, value);
    
        //prefs.commit();
      }
    
      static void logout() {
        prefs.clear();
      }
    }
    

    Now you just need to call it in main function like this

    void main() {
    WidgetsFlutterBinding.ensureInitialized();
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp,
      DeviceOrientation.portraitDown,
    ]).then((_) {
      SharedPreferences.getInstance().then((SharedPreferences sp) {
        SPHelper.setPref(sp);
        setpLocator();
        runApp(MyApp());
      });
    });
    }
    

    now whenever you want to use the shared preference then you just need to do this for setting the value to shared preference SPHelper.setString("ID", value); and SPHelper.getString("ID"); for getting the value from shared preference.

    so basically you don't need to wait tell shared preference is initialized.