set method

Future<void> set(
  1. String key,
  2. 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.");
  }
}