Loading...
Searching...
No Matches
KeyValueStorage Class Referenceabstract

Interface for key-value storage operations, allowing storage and retrieval of various data types. More...

Inherits Finalizable.

Public Member Functions

bool contains (String key)
 
List< String > getKeys ()
 
int getInt (String key, int defaultValue)
 
int getLong (String key, int defaultValue)
 
bool getBool (String key, bool defaultValue)
 
double getFloat (String key, double defaultValue)
 
double getDouble (String key, double defaultValue)
 
String getString (String key, String defaultValue)
 
void putInt (String key, int value)
 
void putLong (String key, int value)
 
void putBool (String key, bool value)
 
void putFloat (String key, double value)
 
void putDouble (String key, double value)
 
void putString (String key, String value)
 
void remove (String key)
 
void clear ()
 

Detailed Description

Interface for key-value storage operations, allowing storage and retrieval of various data types.

Referenced from StorageManager.

Definition at line 18 of file key_value_storage.dart.

Member Function Documentation

◆ clear()

void KeyValueStorage.clear ( )

Removes all entries from the storage.

Dart code snippet:

// Clear all data
_userStorage!.clear();
print("Cleared all data from user storage");

◆ contains()

bool KeyValueStorage.contains ( String key)

Checks whether the key exists in the storage.

Parameters
keyKey to check.
Returns
true if a value is stored under the key (regardless of type).

Dart code snippet:

// Check if keys exist
bool hasUserName = _userStorage!.contains("user_name");
bool hasNonExistent = _userStorage!.contains("non_existent_key");
print("Contains 'user_name': $hasUserName");
print("Contains 'non_existent_key': $hasNonExistent");

◆ getBool()

bool KeyValueStorage.getBool ( String key,
bool defaultValue )

Gets a boolean value. Returns defaultValue if the key is missing or the stored type is not bool.

Parameters
keyLookup key.
defaultValueValue to return when not found or type mismatch.
Returns
Stored bool or defaultValue.

Dart code snippet:

// Retrieve boolean values with defaults
bool isPremium = _userStorage!.getBool("is_premium", false);
bool notificationsEnabled = _userStorage!.getBool(
"notifications_enabled",
true,
);
bool nonExistentBool = _userStorage!.getBool("non_existent_bool", false);
print("Is premium: $isPremium");
print("Notifications enabled: $notificationsEnabled");
print("Non-existent bool: $nonExistentBool");

◆ getDouble()

double KeyValueStorage.getDouble ( String key,
double defaultValue )

Gets a 64-bit floating-point value. Returns defaultValue if the key is missing or the stored type is not double.

Parameters
keyLookup key.
defaultValueValue to return when not found or type mismatch.
Returns
Stored double or defaultValue.

Dart code snippet:

// Retrieve double values with defaults
double userLat = _userStorage!.getDouble("user_location_lat", 0.0);
double userLng = _userStorage!.getDouble("user_location_lng", 0.0);
double nonExistentDouble = _userStorage!.getDouble(
"non_existent_double",
-1.0,
);
print("User location lat: $userLat");
print("User location lng: $userLng");
print("Non-existent double: $nonExistentDouble");

◆ getFloat()

double KeyValueStorage.getFloat ( String key,
double defaultValue )

Gets a 32-bit floating-point value. Returns defaultValue if the key is missing or the stored type is not float.

Parameters
keyLookup key.
defaultValueValue to return when not found or type mismatch.
Returns
Stored float or defaultValue.

Dart code snippet:

// Retrieve float values with defaults
float userRating = _userStorage!.getFloat("user_rating", 0.0);
float temperature = _userStorage!.getFloat("temperature", 0.0);
float nonExistentFloat = _userStorage!.getFloat("non_existent_float", -1.0);
print("User rating: $userRating");
print("Temperature: $temperature");
print("Non-existent float: $nonExistentFloat");

◆ getInt()

int KeyValueStorage.getInt ( String key,
int defaultValue )

Gets a 32-bit integer value. Returns defaultValue if the key is missing or the stored type is not int.

Parameters
keyLookup key.
defaultValueValue to return when not found or type mismatch.
Returns
Stored int32 or defaultValue.

Dart code snippet:

// Retrieve integer values with defaults
int userAge = _userStorage!.getInt("user_age", 0);
int loginCount = _userStorage!.getInt("login_count", 0);
int nonExistentInt = _userStorage!.getInt("non_existent_int", -1);
print("User age: $userAge");
print("Login count: $loginCount");
print("Non-existent int: $nonExistentInt");

◆ getKeys()

List< String > KeyValueStorage.getKeys ( )

Returns all keys currently stored.

Returns
List of keys. Order is implementation-defined (not guaranteed).

Dart code snippet:

// Get all stored keys
List<String> allKeys = _userStorage!.getKeys();
print("All stored keys: $allKeys");

◆ getLong()

int KeyValueStorage.getLong ( String key,
int defaultValue )

Gets a 64-bit integer value. Returns defaultValue if the key is missing or the stored type is not long.

Parameters
keyLookup key.
defaultValueValue to return when not found or type mismatch.
Returns
Stored int64 or defaultValue.

Dart code snippet:

// Retrieve long values with defaults
long regTimestamp = _userStorage!.getLong("registration_timestamp", 0);
long lastLogin = _userStorage!.getLong("last_login_timestamp", 0);
long nonExistentLong = _userStorage!.getLong("non_existent_long", -1);
print("Registration timestamp: $regTimestamp");
print("Last login timestamp: $lastLogin");
print("Non-existent long: $nonExistentLong");

◆ getString()

String KeyValueStorage.getString ( String key,
String defaultValue )

Gets a string value. Returns defaultValue if the key is missing or the stored type is not string.

Parameters
keyLookup key.
defaultValueValue to return when not found or type mismatch.
Returns
Stored string or defaultValue.

Dart code snippet:

// Retrieve string values with defaults
String userName = _userStorage!.getString("user_name", "Unknown");
String userEmail = _userStorage!.getString("user_email", "");
String nonExistent = _userStorage!.getString(
"non_existent_key",
"default_value",
);
print("User name: $userName");
print("User email: $userEmail");
print("Non-existent key: $nonExistent");

◆ putBool()

void KeyValueStorage.putBool ( String key,
bool value )

Stores a boolean value under the key, replacing any existing value.

Parameters
keyKey to set.
valueValue to store.

Dart code snippet:

// Store boolean values
_userStorage!.putBool("is_premium", true);
_userStorage!.putBool("notifications_enabled", false);
print("Stored boolean values");

◆ putDouble()

void KeyValueStorage.putDouble ( String key,
double value )

Stores a 64-bit floating-point value under the key, replacing any existing value.

Parameters
keyKey to set.
valueValue to store.

Dart code snippet:

// Store double values
_userStorage!.putDouble("user_location_lat", 55.7558);
_userStorage!.putDouble("user_location_lng", 37.6176);
print("Stored double values");

◆ putFloat()

void KeyValueStorage.putFloat ( String key,
double value )

Stores a 32-bit floating-point value under the key, replacing any existing value.

Parameters
keyKey to set.
valueValue to store.

Dart code snippet:

// Store float values
_userStorage!.putFloat("user_rating", 4.5);
_userStorage!.putFloat("temperature", 23.5);
print("Stored float values");

◆ putInt()

void KeyValueStorage.putInt ( String key,
int value )

Stores a 32-bit integer value under the key, replacing any existing value.

Parameters
keyKey to set.
valueValue to store.

Dart code snippet:

// Store integer values
_userStorage!.putInt("user_age", 25);
_userStorage!.putInt("login_count", 42);
print("Stored integer values");

◆ putLong()

void KeyValueStorage.putLong ( String key,
int value )

Stores a 64-bit integer value under the key, replacing any existing value.

Parameters
keyKey to set.
valueValue to store.

Dart code snippet:

// Store long values
_userStorage!.putLong("registration_timestamp", 1640995200000);
_userStorage!.putLong(
"last_login_timestamp",
DateTime.now().millisecondsSinceEpoch,
);
print("Stored long values");

◆ putString()

void KeyValueStorage.putString ( String key,
String value )

Stores a string value under the key, replacing any existing value.

Parameters
keyKey to set.
valueValue to store.

Dart code snippet:

// Store string values
_userStorage!.putString("user_name", "John Doe");
_userStorage!.putString("user_email", "john.doe@example.com");
print("Stored string values");

◆ remove()

void KeyValueStorage.remove ( String key)

Removes a value by key. No-op if the key does not exist.

Parameters
keyKey to remove.

Dart code snippet:

// Remove specific keys
_userStorage!.remove("user_age");
_userStorage!.remove("non_existent_key"); // No-op
print("Removed 'user_age' key");

The documentation for this class was generated from the following file: