Prefs class

A singleton-style utility class for accessing SharedPreferences in a simplified way.

Call Prefs.instance once before runApp() to initialize.

Example:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Prefs.instance();
  runApp(MyApp());
}

final name = Prefs.getString('username');
await Prefs.setBool('isLoggedIn', true);

Constructors

Prefs()
Creates a new instance of Prefs.

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

clear() Future<bool>
Clears all keys and values from preferences.
contains(String key) bool
Returns true if the given key exists in preferences.
getBool(String key, {bool defaultValue = false}) bool
Returns a bool value associated with the given key, or defaultValue if the key is not found.
getDouble(String key, {double defaultValue = 0.0}) double
Returns a double value associated with the given key, or defaultValue if the key is not found.
getInt(String key, {int defaultValue = 0}) int
Returns an int value associated with the given key, or defaultValue if the key is not found.
getString(String key, {String defaultValue = ''}) String
Returns a String value associated with the given key, or defaultValue if the key is not found.
getStringList(String key, {List<String> defaultValue = const []}) List<String>
Returns a list of String values associated with the given key, or defaultValue if the key is not found.
instance() Future<void>
Initializes the SharedPreferences instance.
remove(String key) Future<bool>
Removes the value associated with the given key.
setBool(String key, bool value) Future<bool>
Sets a bool value for the given key.
setDouble(String key, double value) Future<bool>
Sets a double value for the given key.
setInt(String key, int value) Future<bool>
Sets an int value for the given key.
setString(String key, String value) Future<bool>
Sets a String value for the given key.
setStringList(String key, List<String> value) Future<bool>
Sets a list of String values for the given key.