I want to save user preferences using Flutter\'s SharedPreference
. But the registered preferences are ALL null at new start (when app have been closed, not unis
import 'package:shared_preferences/shared_preferences.dart';
class MySharedPreferences {
MySharedPreferences._privateConstructor();
static final MySharedPreferences instance =
MySharedPreferences._privateConstructor();
setStringValue(String key, String value) async {
SharedPreferences myPrefs = await SharedPreferences.getInstance();
myPrefs.setString(key, value);
}
Future getStringValue(String key) async {
SharedPreferences myPrefs = await SharedPreferences.getInstance();
return myPrefs.getString(key) ?? "";
}
Future containsKey(String key) async {
SharedPreferences myPrefs = await SharedPreferences.getInstance();
return myPrefs.containsKey(key);
}
removeValue(String key) async {
SharedPreferences myPrefs = await SharedPreferences.getInstance();
return myPrefs.remove(key);
}
removeAll() async{
SharedPreferences myPrefs = await SharedPreferences.getInstance();
return myPrefs.clear();
}
}
For more reference: Flutter Shared Preferences Tutorial