Integrate shared preferences for persistent key-value storage.

This commit is contained in:
2025-09-21 11:37:02 +02:00
parent 5572c66b10
commit a637becac0
4 changed files with 288 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
import 'package:fluttery/fluttery.dart';
/// providing methods for managing persistent storage of key-value pairs.
abstract class Preferences implements Service {
/// Stores a string value with the given [key].
Future<void> setString(String key, String value);
/// Retrieves the string value associated with the given [key].
Future<String?> getString(String key);
/// Stores an integer value with the given [key].
Future<void> setInt(String key, int value);
/// Retrieves the integer value associated with the given [key].
Future<int?> getInt(String key);
/// Stores a boolean value with the given [key].
Future<void> setBool(String key, bool value);
/// Retrieves the boolean value associated with the given [key].
Future<bool?> getBool(String key);
/// Stores a double value with the given [key].
Future<void> setDouble(String key, double value);
/// Retrieves the double value associated with the given [key].
Future<double?> getDouble(String key);
/// Stores a list of strings with the given [key].
Future<void> setStringList(String key, List<String> value);
/// Retrieves the list of strings associated with the given [key].
Future<List<String>?> getStringList(String key);
/// Removes the key-value pair associated with the given [key].
Future<void> remove(String key);
/// Clears all key-value pairs in the preferences.
Future<void> clear();
}

View File

@@ -0,0 +1,70 @@
import 'package:flutter/material.dart';
import 'package:fluttery/preferences/preferences.dart';
import 'package:shared_preferences/shared_preferences.dart';
class PreferencesImpl implements Preferences {
final SharedPreferences _prefs;
/// Constructor
PreferencesImpl({required SharedPreferences instance}) : _prefs = instance;
@override
Future<void> setString(String key, String value) async {
await _prefs.setString(key, value);
}
@override
Future<String?> getString(String key) async {
return _prefs.getString(key);
}
@override
Future<void> setInt(String key, int value) async {
await _prefs.setInt(key, value);
}
@override
Future<int?> getInt(String key) async {
return _prefs.getInt(key);
}
@override
Future<void> setBool(String key, bool value) async {
await _prefs.setBool(key, value);
}
@override
Future<bool?> getBool(String key) async {
return _prefs.getBool(key);
}
@override
Future<void> setDouble(String key, double value) async {
await _prefs.setDouble(key, value);
}
@override
Future<double?> getDouble(String key) async {
return _prefs.getDouble(key);
}
@override
Future<void> setStringList(String key, List<String> value) async {
await _prefs.setStringList(key, value);
}
@override
Future<List<String>?> getStringList(String key) async {
return _prefs.getStringList(key);
}
@override
Future<void> remove(String key) async {
await _prefs.remove(key);
}
@override
Future<void> clear() async {
await _prefs.clear();
}
}