Skip to content

Instantly share code, notes, and snippets.

@rubywai
Created May 20, 2023 06:49
Show Gist options
  • Save rubywai/7ad4e1439437a3f93498391830a78824 to your computer and use it in GitHub Desktop.
Save rubywai/7ad4e1439437a3f93498391830a78824 to your computer and use it in GitHub Desktop.
import 'package:shared_preferences/shared_preferences.dart';
class MyStorage{
Future<SharedPreferences> _sharedPreference ()async{
return await SharedPreferences.getInstance();
}
void saveString({required String key,required String? value}) async{
SharedPreferences sharedPreferences = await _sharedPreference();
if(value != null) {
sharedPreferences.setString(key, value);
}
}
Future<String> getString(String key)async{
SharedPreferences sharedPreferences = await _sharedPreference();
String? value = sharedPreferences.getString(key);
return value ?? '';
}
void saveBool({required String key,required bool value}) async{
SharedPreferences sharedPreferences = await _sharedPreference();
sharedPreferences.setBool(key, value);
}
Future<bool> getBool(String key) async{
SharedPreferences sharedPreferences = await _sharedPreference();
return sharedPreferences.getBool(key) ?? false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment