Skip to content

Instantly share code, notes, and snippets.

@sokhasen
Created January 3, 2020 04:15
Show Gist options
  • Save sokhasen/42ce6b3b3daba2f7229b59bbcaf1c8bf to your computer and use it in GitHub Desktop.
Save sokhasen/42ce6b3b3daba2f7229b59bbcaf1c8bf to your computer and use it in GitHub Desktop.
flutter share_preference utils
import 'dart:async';
import 'package:shared_preferences/shared_preferences.dart';
class Preference {
Preference._();
static Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
/// ----------------------------------------------------------
/// Generic routine to fetch an application preference
/// ----------------------------------------------------------
static Future<String> getItem(String name) async {
final SharedPreferences prefs = await _prefs;
return prefs.getString(name) ?? '';
}
/// ----------------------------------------------------------
/// Generic routine to saves an application preference
/// ----------------------------------------------------------
static Future<bool> setItem(String name, String value) async {
final SharedPreferences prefs = await _prefs;
return prefs.setString(name, value);
}
static Future<Set<String>> getAll() async {
final SharedPreferences prefs = await _prefs;
return prefs.getKeys();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment