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);
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
trueif the givenkeyexists in preferences. -
getBool(
String key, {bool defaultValue = false}) → bool -
Returns a
boolvalue associated with the givenkey, ordefaultValueif the key is not found. -
getDouble(
String key, {double defaultValue = 0.0}) → double -
Returns a
doublevalue associated with the givenkey, ordefaultValueif the key is not found. -
getInt(
String key, {int defaultValue = 0}) → int -
Returns an
intvalue associated with the givenkey, ordefaultValueif the key is not found. -
getString(
String key, {String defaultValue = ''}) → String -
Returns a
Stringvalue associated with the givenkey, ordefaultValueif the key is not found. -
getStringList(
String key, {List< String> defaultValue = const []}) → List<String> -
Returns a list of
Stringvalues associated with the givenkey, ordefaultValueif the key is not found. -
instance(
) → Future< void> -
Initializes the
SharedPreferencesinstance. -
remove(
String key) → Future< bool> -
Removes the value associated with the given
key. -
setBool(
String key, bool value) → Future< bool> -
Sets a
boolvalue for the givenkey. -
setDouble(
String key, double value) → Future< bool> -
Sets a
doublevalue for the givenkey. -
setInt(
String key, int value) → Future< bool> -
Sets an
intvalue for the givenkey. -
setString(
String key, String value) → Future< bool> -
Sets a
Stringvalue for the givenkey. -
setStringList(
String key, List< String> value) → Future<bool> -
Sets a list of
Stringvalues for the givenkey.