set method
- String key,
- dynamic value
Sets the value for a given key in SharedPreferences.
This method automatically determines the correct storage type based on the key. If the key does not exist, an exception is thrown.
key: The key for the value to be set.value: The value to store.
Implementation
Future<void> set(String key, dynamic value) async {
await ensureSharedPrefIsInitialized();
if (intKeys.containsKey(key)) {
setInt(key, value);
} else if (boolKeys.containsKey(key)) {
setBool(key, value);
} else if (doubleKeys.containsKey(key)) {
setDouble(key, value);
} else if (stringKeys.containsKey(key)) {
setString(key, value);
} else if (stringListKeys.containsKey(key)) {
setStringList(key, value);
} else {
throw Exception("Key '$key' is not implemented.");
}
}